Skip to content

Commit 348aa24

Browse files
authored
Use trait so it is re-usable in other situations (#5)
1 parent fe0fa68 commit 348aa24

File tree

3 files changed

+103
-90
lines changed

3 files changed

+103
-90
lines changed

config/verify.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
use LycheeVerify\Validators\ValidateSupporter;
88
use LycheeVerify\Verify;
99
use LycheeVerify\VerifyServiceProvider;
10+
use LycheeVerify\VerifyTrait;
1011

1112
return [
1213
'validation' => [
1314
ValidateSupporter::class => 'ef1a42701af6dc36e052556a0ee1c762394f9428',
1415
ValidatePro::class => '482b48f1a026684b6c1754e45ca180ffc52483ff',
1516
ValidateSignature::class => '5a8a855d4b59c44c298daa66801c79f2aba20492',
16-
Verify::class => 'ffd01909f5189bc7bae21266e356f83c898ccd37',
17+
Verify::class => 'aba420193a9d017e36a215ef8dbd6332bd2c2525',
1718
VerifySupporterStatus::class => '6358c45ed0414c1e2697e0881238659fa6221bed',
1819
VerifyProStatus::class => '212e6ada794587ee8e2b81cf76e243d134a7e823',
1920
VerifyServiceProvider::class => '923b63b15d25e69b95ed1d5ec1c82ba57f1a7d74',
21+
VerifyTrait::class => '1a0679c1d63b209b9427de6073e48bb1246e02a4',
2022
],
2123
];

src/Verify.php

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Support\Facades\DB;
66
use LycheeVerify\Contract\Status;
77
use LycheeVerify\Contract\VerifyInterface;
8-
use LycheeVerify\Exceptions\SupporterOnlyOperationException;
98
use LycheeVerify\Validators\ValidatePro;
109
use LycheeVerify\Validators\ValidateSignature;
1110
use LycheeVerify\Validators\ValidateSupporter;
@@ -14,6 +13,7 @@
1413

1514
class Verify implements VerifyInterface
1615
{
16+
use VerifyTrait;
1717
private string $config_email;
1818
private string $license_key;
1919
private ValidateSignature $validateSignature;
@@ -58,94 +58,6 @@ public function get_status(): Status
5858
return Status::FREE_EDITION;
5959
}
6060

61-
/**
62-
* Check the status of the installation and validate.
63-
*
64-
* @param Status $required_status (default to SUPPORTER_EDITION)
65-
*
66-
* @return bool
67-
*/
68-
public function check(Status $required_status = Status::SUPPORTER_EDITION): bool
69-
{
70-
if ($required_status === Status::FREE_EDITION) {
71-
return true;
72-
}
73-
74-
$status = $this->get_status();
75-
76-
return match ($status) {
77-
Status::SIGNATURE_EDITION => true,
78-
Status::PRO_EDITION => in_array($required_status, [Status::PRO_EDITION, Status::SUPPORTER_EDITION], true),
79-
Status::SUPPORTER_EDITION => in_array($required_status, [Status::SUPPORTER_EDITION], true),
80-
default => false,
81-
};
82-
}
83-
84-
/**
85-
* Returns true if the user is a supporter (or plus registered user).
86-
*
87-
* @return bool
88-
*/
89-
public function is_supporter(): bool
90-
{
91-
return $this->check(Status::SUPPORTER_EDITION);
92-
}
93-
94-
/**
95-
* Return true of the user is a plus registered user.
96-
*
97-
* @return bool
98-
*/
99-
public function is_pro(): bool
100-
{
101-
return $this->check(Status::PRO_EDITION);
102-
}
103-
104-
/**
105-
* Return true if the user is a signature user.
106-
*
107-
* @return bool
108-
*/
109-
public function is_signature(): bool
110-
{
111-
return $this->check(Status::SIGNATURE_EDITION);
112-
}
113-
114-
/**
115-
* Authorize the operation if the installation is verified.
116-
* Otherwise throw an exception.
117-
*
118-
* @param Status $required_status (default to SUPPORTER_EDITION)
119-
*
120-
* @return void
121-
*
122-
* @throws SupporterOnlyOperationException
123-
*/
124-
public function authorize(Status $required_status = Status::SUPPORTER_EDITION): void
125-
{
126-
if (!$this->check($required_status)) {
127-
throw new SupporterOnlyOperationException($required_status);
128-
}
129-
}
130-
131-
/**
132-
* Fork depending whether the installation is verified or not.
133-
*
134-
* @template T
135-
*
136-
* @param T|\Closure(): T $valIfTrue what happens or Value if we features are enabled
137-
* @param T|\Closure(): T $valIfFalse what happens or Value if we features are disabled
138-
* @param Status $required_status
139-
*
140-
* @return T
141-
*/
142-
public function when(mixed $valIfTrue, mixed $valIfFalse, Status $required_status = Status::SUPPORTER_EDITION): mixed
143-
{
144-
$retValue = $this->check($required_status) ? $valIfTrue : $valIfFalse;
145-
146-
return is_callable($retValue) ? $retValue() : $retValue;
147-
}
148-
14961
/**
15062
* Validate installation.
15163
*

src/VerifyTrait.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace LycheeVerify;
4+
5+
use LycheeVerify\Contract\Status;
6+
use LycheeVerify\Exceptions\SupporterOnlyOperationException;
7+
8+
trait VerifyTrait
9+
{
10+
abstract public function get_status(): Status;
11+
12+
/**
13+
* Check the status of the installation and validate.
14+
*
15+
* @param Status $required_status (default to SUPPORTER_EDITION)
16+
*
17+
* @return bool
18+
*/
19+
public function check(Status $required_status = Status::SUPPORTER_EDITION): bool
20+
{
21+
if ($required_status === Status::FREE_EDITION) {
22+
return true;
23+
}
24+
25+
$status = $this->get_status();
26+
27+
return match ($status) {
28+
Status::SIGNATURE_EDITION => true,
29+
Status::PRO_EDITION => in_array($required_status, [Status::PRO_EDITION, Status::SUPPORTER_EDITION], true),
30+
Status::SUPPORTER_EDITION => in_array($required_status, [Status::SUPPORTER_EDITION], true),
31+
default => false,
32+
};
33+
}
34+
35+
/**
36+
* Returns true if the user is a supporter (or plus registered user).
37+
*
38+
* @return bool
39+
*/
40+
public function is_supporter(): bool
41+
{
42+
return $this->check(Status::SUPPORTER_EDITION);
43+
}
44+
45+
/**
46+
* Return true of the user is a plus registered user.
47+
*
48+
* @return bool
49+
*/
50+
public function is_pro(): bool
51+
{
52+
return $this->check(Status::PRO_EDITION);
53+
}
54+
55+
/**
56+
* Return true if the user is a signature user.
57+
*
58+
* @return bool
59+
*/
60+
public function is_signature(): bool
61+
{
62+
return $this->check(Status::SIGNATURE_EDITION);
63+
}
64+
65+
/**
66+
* Authorize the operation if the installation is verified.
67+
* Otherwise throw an exception.
68+
*
69+
* @param Status $required_status (default to SUPPORTER_EDITION)
70+
*
71+
* @return void
72+
*
73+
* @throws SupporterOnlyOperationException
74+
*/
75+
public function authorize(Status $required_status = Status::SUPPORTER_EDITION): void
76+
{
77+
if (!$this->check($required_status)) {
78+
throw new SupporterOnlyOperationException($required_status);
79+
}
80+
}
81+
82+
/**
83+
* Fork depending whether the installation is verified or not.
84+
*
85+
* @template T
86+
*
87+
* @param T|\Closure(): T $valIfTrue what happens or Value if we features are enabled
88+
* @param T|\Closure(): T $valIfFalse what happens or Value if we features are disabled
89+
* @param Status $required_status
90+
*
91+
* @return T
92+
*/
93+
public function when(mixed $valIfTrue, mixed $valIfFalse, Status $required_status = Status::SUPPORTER_EDITION): mixed
94+
{
95+
$retValue = $this->check($required_status) ? $valIfTrue : $valIfFalse;
96+
97+
return is_callable($retValue) ? $retValue() : $retValue;
98+
}
99+
}

0 commit comments

Comments
 (0)