33namespace LycheeVerify ;
44
55use Illuminate \Support \Facades \DB ;
6+ use Illuminate \Support \Facades \Schema ;
67use LycheeVerify \Contract \Status ;
78use LycheeVerify \Contract \VerifyInterface ;
89use LycheeVerify \Exceptions \SupporterOnlyOperationException ;
@@ -23,18 +24,54 @@ class Verify implements VerifyInterface
2324
2425 private ?Status $ status ;
2526
27+ private bool $ initialized = false ;
28+
2629 public function __construct (
2730 #[\SensitiveParameter] ?string $ config_email = null ,
2831 #[\SensitiveParameter] ?string $ license_key = null ,
2932 #[\SensitiveParameter] ?string $ public_key = null ,
3033 #[\SensitiveParameter] ?string $ hash_supporter = null ,
3134 #[\SensitiveParameter] ?string $ hash_pro = null ,
3235 ) {
33- $ this ->config_email = $ config_email ?? DB ::table ('configs ' )->where ('key ' , 'email ' )->first ()?->value ?? '' ; // @phpstan-ignore-line
34- $ this ->license_key = $ license_key ?? DB ::table ('configs ' )->where ('key ' , 'license_key ' )->first ()?->value ?? '' ; // @phpstan-ignore-line
3536 $ this ->validateSignature = new ValidateSignature ($ public_key );
3637 $ this ->validatePro = new ValidatePro ($ hash_pro );
3738 $ this ->validateSupporter = new ValidateSupporter ($ hash_supporter );
39+ $ this ->init ($ config_email , $ license_key );
40+ }
41+
42+ /**
43+ * To avoid crashing when the database does not exists we
44+ * add some safeties.
45+ *
46+ * @param string|null $config_email
47+ * @param string|null $license_key
48+ *
49+ * @return bool
50+ */
51+ private function init (
52+ #[\SensitiveParameter] ?string $ config_email = null ,
53+ #[\SensitiveParameter] ?string $ license_key = null ,
54+ ): bool {
55+ if ($ config_email !== null || $ license_key !== null ) {
56+ // If both values are provided, no need to check the database
57+ $ this ->config_email = $ config_email ?? '' ;
58+ $ this ->license_key = $ license_key ?? '' ;
59+ $ this ->initialized = true ;
60+
61+ return true ;
62+ }
63+
64+ // Validate that the database is ready
65+ if (!Schema::hasTable ('configs ' )) {
66+ return false ;
67+ }
68+
69+ // Load the necessary config entries
70+ $ this ->config_email = DB ::table ('configs ' )->where ('key ' , 'email ' )->first ()?->value ?? '' ; // @phpstan-ignore-line
71+ $ this ->license_key = DB ::table ('configs ' )->where ('key ' , 'license_key ' )->first ()?->value ?? '' ; // @phpstan-ignore-line
72+ $ this ->initialized = true ;
73+
74+ return true ;
3875 }
3976
4077 /**
@@ -44,6 +81,10 @@ public function __construct(
4481 */
4582 public function get_status (): Status
4683 {
84+ if (!$ this ->initialized && !$ this ->init ()) {
85+ return Status::FREE_EDITION ;
86+ }
87+
4788 return $ this ->status ??= $ this ->resolve_status ();
4889 }
4990
0 commit comments