diff --git a/config/verify.php b/config/verify.php index ccc8af3..b46b3ec 100644 --- a/config/verify.php +++ b/config/verify.php @@ -7,15 +7,17 @@ use LycheeVerify\Validators\ValidateSupporter; use LycheeVerify\Verify; use LycheeVerify\VerifyServiceProvider; +use LycheeVerify\VerifyTrait; return [ 'validation' => [ ValidateSupporter::class => 'ef1a42701af6dc36e052556a0ee1c762394f9428', ValidatePro::class => '482b48f1a026684b6c1754e45ca180ffc52483ff', ValidateSignature::class => '5a8a855d4b59c44c298daa66801c79f2aba20492', - Verify::class => 'ffd01909f5189bc7bae21266e356f83c898ccd37', + Verify::class => 'aba420193a9d017e36a215ef8dbd6332bd2c2525', VerifySupporterStatus::class => '6358c45ed0414c1e2697e0881238659fa6221bed', VerifyProStatus::class => '212e6ada794587ee8e2b81cf76e243d134a7e823', VerifyServiceProvider::class => '923b63b15d25e69b95ed1d5ec1c82ba57f1a7d74', + VerifyTrait::class => '1a0679c1d63b209b9427de6073e48bb1246e02a4', ], ]; diff --git a/src/Verify.php b/src/Verify.php index df358e7..7dde5d9 100755 --- a/src/Verify.php +++ b/src/Verify.php @@ -5,7 +5,6 @@ use Illuminate\Support\Facades\DB; use LycheeVerify\Contract\Status; use LycheeVerify\Contract\VerifyInterface; -use LycheeVerify\Exceptions\SupporterOnlyOperationException; use LycheeVerify\Validators\ValidatePro; use LycheeVerify\Validators\ValidateSignature; use LycheeVerify\Validators\ValidateSupporter; @@ -14,6 +13,7 @@ class Verify implements VerifyInterface { + use VerifyTrait; private string $config_email; private string $license_key; private ValidateSignature $validateSignature; @@ -58,94 +58,6 @@ public function get_status(): Status return Status::FREE_EDITION; } - /** - * Check the status of the installation and validate. - * - * @param Status $required_status (default to SUPPORTER_EDITION) - * - * @return bool - */ - public function check(Status $required_status = Status::SUPPORTER_EDITION): bool - { - if ($required_status === Status::FREE_EDITION) { - return true; - } - - $status = $this->get_status(); - - return match ($status) { - Status::SIGNATURE_EDITION => true, - Status::PRO_EDITION => in_array($required_status, [Status::PRO_EDITION, Status::SUPPORTER_EDITION], true), - Status::SUPPORTER_EDITION => in_array($required_status, [Status::SUPPORTER_EDITION], true), - default => false, - }; - } - - /** - * Returns true if the user is a supporter (or plus registered user). - * - * @return bool - */ - public function is_supporter(): bool - { - return $this->check(Status::SUPPORTER_EDITION); - } - - /** - * Return true of the user is a plus registered user. - * - * @return bool - */ - public function is_pro(): bool - { - return $this->check(Status::PRO_EDITION); - } - - /** - * Return true if the user is a signature user. - * - * @return bool - */ - public function is_signature(): bool - { - return $this->check(Status::SIGNATURE_EDITION); - } - - /** - * Authorize the operation if the installation is verified. - * Otherwise throw an exception. - * - * @param Status $required_status (default to SUPPORTER_EDITION) - * - * @return void - * - * @throws SupporterOnlyOperationException - */ - public function authorize(Status $required_status = Status::SUPPORTER_EDITION): void - { - if (!$this->check($required_status)) { - throw new SupporterOnlyOperationException($required_status); - } - } - - /** - * Fork depending whether the installation is verified or not. - * - * @template T - * - * @param T|\Closure(): T $valIfTrue what happens or Value if we features are enabled - * @param T|\Closure(): T $valIfFalse what happens or Value if we features are disabled - * @param Status $required_status - * - * @return T - */ - public function when(mixed $valIfTrue, mixed $valIfFalse, Status $required_status = Status::SUPPORTER_EDITION): mixed - { - $retValue = $this->check($required_status) ? $valIfTrue : $valIfFalse; - - return is_callable($retValue) ? $retValue() : $retValue; - } - /** * Validate installation. * diff --git a/src/VerifyTrait.php b/src/VerifyTrait.php new file mode 100644 index 0000000..12471ad --- /dev/null +++ b/src/VerifyTrait.php @@ -0,0 +1,99 @@ +get_status(); + + return match ($status) { + Status::SIGNATURE_EDITION => true, + Status::PRO_EDITION => in_array($required_status, [Status::PRO_EDITION, Status::SUPPORTER_EDITION], true), + Status::SUPPORTER_EDITION => in_array($required_status, [Status::SUPPORTER_EDITION], true), + default => false, + }; + } + + /** + * Returns true if the user is a supporter (or plus registered user). + * + * @return bool + */ + public function is_supporter(): bool + { + return $this->check(Status::SUPPORTER_EDITION); + } + + /** + * Return true of the user is a plus registered user. + * + * @return bool + */ + public function is_pro(): bool + { + return $this->check(Status::PRO_EDITION); + } + + /** + * Return true if the user is a signature user. + * + * @return bool + */ + public function is_signature(): bool + { + return $this->check(Status::SIGNATURE_EDITION); + } + + /** + * Authorize the operation if the installation is verified. + * Otherwise throw an exception. + * + * @param Status $required_status (default to SUPPORTER_EDITION) + * + * @return void + * + * @throws SupporterOnlyOperationException + */ + public function authorize(Status $required_status = Status::SUPPORTER_EDITION): void + { + if (!$this->check($required_status)) { + throw new SupporterOnlyOperationException($required_status); + } + } + + /** + * Fork depending whether the installation is verified or not. + * + * @template T + * + * @param T|\Closure(): T $valIfTrue what happens or Value if we features are enabled + * @param T|\Closure(): T $valIfFalse what happens or Value if we features are disabled + * @param Status $required_status + * + * @return T + */ + public function when(mixed $valIfTrue, mixed $valIfFalse, Status $required_status = Status::SUPPORTER_EDITION): mixed + { + $retValue = $this->check($required_status) ? $valIfTrue : $valIfFalse; + + return is_callable($retValue) ? $retValue() : $retValue; + } +}