Issues (21)

src/Config/Traits/CanInitFromConfigTrait.php (4 issues)

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
It seems like setEnabled() 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 ignore-call  annotation

54
        $this->/** @scrutinizer ignore-call */ 
55
               setEnabled(static::getConfigVar('enabled'));
Loading history...
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 ignore-call  annotation

55
        $this->/** @scrutinizer ignore-call */ 
56
               setSiteKey(static::getConfigVar('site_key'));
Loading history...
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 ignore-call  annotation

56
        $this->/** @scrutinizer ignore-call */ 
57
               setSecretKey(static::getConfigVar('secret_key'));
Loading history...
57
    }
58
59
    /**
60
     * @param string $value
61
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
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