Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
90 changes: 1 addition & 89 deletions src/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +13,7 @@

class Verify implements VerifyInterface
{
use VerifyTrait;
private string $config_email;
private string $license_key;
private ValidateSignature $validateSignature;
Expand Down Expand Up @@ -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.
*
Expand Down
99 changes: 99 additions & 0 deletions src/VerifyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace LycheeVerify;

use LycheeVerify\Contract\Status;
use LycheeVerify\Exceptions\SupporterOnlyOperationException;

trait VerifyTrait
{
abstract public function get_status(): Status;

/**
* 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;
}
}