Issues (21)

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

1
<?php
2
3
namespace ByTIC\GoogleRecaptcha\Config\Traits;
4
5
use ByTIC\GoogleRecaptcha\Config\Config;
6
use ByTIC\GoogleRecaptcha\Config\ConfigEnv;
7
8
/**
9
 * Trait CanInitFromEnviromentTrait
10
 * @package ByTIC\GoogleRecaptcha\Config\Traits
11
 */
12
trait CanInitFromEnviromentTrait
13
{
14
15
    /**
16
     * @return static|Config
17
     */
18
    public static function fromEnv()
19
    {
20
        $config = new static();
21
        $config->initFromEnv();
22
        return $config;
23
    }
24
25
    /**
26
     * @return bool
27
     */
28
    public static function canInitFromEnv()
29
    {
30
        if (empty(static::getEnvVar(ConfigEnv::SITE_KEY))) {
31
            return false;
32
        }
33
        if (empty(static::getEnvVar(ConfigEnv::SECRET_KEY))) {
34
            return false;
35
        }
36
        return true;
37
    }
38
39
    protected function initFromEnv()
40
    {
41
        $this->setEnabled(static::getEnvVar(ConfigEnv::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

41
        $this->/** @scrutinizer ignore-call */ 
42
               setEnabled(static::getEnvVar(ConfigEnv::ENABLED));
Loading history...
42
        $this->setSiteKey(static::getEnvVar(ConfigEnv::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

42
        $this->/** @scrutinizer ignore-call */ 
43
               setSiteKey(static::getEnvVar(ConfigEnv::SITE_KEY));
Loading history...
43
        $this->setSecretKey(static::getEnvVar(ConfigEnv::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

43
        $this->/** @scrutinizer ignore-call */ 
44
               setSecretKey(static::getEnvVar(ConfigEnv::SECRET_KEY));
Loading history...
44
    }
45
46
    /**
47
     * @param string $value
48
     * @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...
49
     * @return mixed|null
50
     */
51
    protected static function getEnvVar($value, $default = null)
52
    {
53
        if (isset($_ENV[$value])) {
54
            return $_ENV[$value];
55
        }
56
        return $default;
57
    }
58
}
59