1 | <?php |
||||||
2 | |||||||
3 | namespace ByTIC\GoogleRecaptcha\Config\Traits; |
||||||
4 | |||||||
5 | use ByTIC\GoogleRecaptcha\Config\Config; |
||||||
6 | use Nip\Container\Container; |
||||||
7 | |||||||
8 | /** |
||||||
9 | * Trait CanInitFromConfigTrait |
||||||
10 | * @package ByTIC\GoogleRecaptcha\Config\Traits |
||||||
11 | */ |
||||||
12 | trait CanInitFromConfigTrait |
||||||
13 | { |
||||||
14 | protected static $configRoot = null; |
||||||
15 | |||||||
16 | /** |
||||||
17 | * @return static|Config |
||||||
18 | */ |
||||||
19 | public static function fromConfig() |
||||||
20 | { |
||||||
21 | $config = new static(); |
||||||
22 | $config->initFromConfig(); |
||||||
23 | return $config; |
||||||
24 | } |
||||||
25 | |||||||
26 | /** |
||||||
27 | * @return bool |
||||||
28 | */ |
||||||
29 | public static function canInitFromConfig(): bool |
||||||
30 | { |
||||||
31 | if (!function_exists('config')) { |
||||||
32 | return false; |
||||||
33 | } |
||||||
34 | |||||||
35 | $container = function_exists('app') ? app() : Container::getInstance(); |
||||||
36 | if (!$container->has('config')) { |
||||||
37 | return false; |
||||||
38 | } |
||||||
39 | |||||||
40 | $config = config(); |
||||||
41 | if ($config->has('recaptcha')) { |
||||||
42 | static::$configRoot = 'recaptcha'; |
||||||
43 | return true; |
||||||
44 | } |
||||||
45 | if ($config->has('services.recaptcha')) { |
||||||
46 | static::$configRoot = 'services.recaptcha'; |
||||||
47 | return true; |
||||||
48 | } |
||||||
49 | return false; |
||||||
50 | } |
||||||
51 | |||||||
52 | protected function initFromConfig() |
||||||
53 | { |
||||||
54 | $this->setEnabled(static::getConfigVar('enabled')); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
55 | $this->setSiteKey(static::getConfigVar('site_key')); |
||||||
0 ignored issues
–
show
It seems like
setSiteKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
56 | $this->setSecretKey(static::getConfigVar('secret_key')); |
||||||
0 ignored issues
–
show
It seems like
setSecretKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
57 | } |
||||||
58 | |||||||
59 | /** |
||||||
60 | * @param string $value |
||||||
61 | * @param null $default |
||||||
0 ignored issues
–
show
|
|||||||
62 | * @return mixed|null |
||||||
63 | */ |
||||||
64 | protected static function getConfigVar($value, $default = null) |
||||||
65 | { |
||||||
66 | $config = config(); |
||||||
67 | return $config->get(static::$configRoot.'.'.$value, $default); |
||||||
68 | } |
||||||
69 | } |
||||||
70 |