TraefikPassword   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toEnv() 0 3 1
A fromEnv() 0 3 1
A __toString() 0 3 1
A optionName() 0 3 1
A __construct() 0 9 1
A fromString() 0 3 1
A toString() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Template\Traefik;
4
5
use Assert\Assertion;
6
use Cocotte\Environment\FromEnvLazyFactory;
7
use Cocotte\Environment\LazyEnvironmentValue;
8
use Cocotte\Environment\LazyExportableOption;
9
use Cocotte\Shell\Env;
10
11
class TraefikPassword implements LazyExportableOption, FromEnvLazyFactory
12
{
13
    const TRAEFIK_UI_PASSWORD = 'TRAEFIK_UI_PASSWORD';
14
    const OPTION_NAME = 'traefik-ui-password';
15
    const REGEX = '/^[a-zA-Z0-9_@#%?&*+=!-]+$/';
16
    /**
17
     * @var string
18
     */
19
    private $value;
20
21
    public function __construct(string $value)
22
    {
23
        Assertion::notEmpty($value, "The Traefik Ui password is empty");
24
        Assertion::regex(
25
            $value,
26
            self::REGEX,
27
            "The Traefik Ui password does not contain only alphanumeric characters and _@#%?&*+=!-"
28
        );
29
        $this->value = $value;
30
    }
31
32
    public static function fromString(string $value): self
33
    {
34
        return new self($value);
35
    }
36
37
    /**
38
     * @param Env $env
39
     * @return LazyEnvironmentValue|self
40
     */
41
    public static function fromEnv(Env $env): LazyEnvironmentValue
42
    {
43
        return new self($env->get(self::TRAEFIK_UI_PASSWORD, ""));
0 ignored issues
show
Bug introduced by
It seems like $env->get(self::TRAEFIK_UI_PASSWORD, '') can also be of type null; however, parameter $value of Cocotte\Template\Traefik...Password::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        return new self(/** @scrutinizer ignore-type */ $env->get(self::TRAEFIK_UI_PASSWORD, ""));
Loading history...
44
    }
45
46
    public static function toEnv(string $value, Env $env): void
47
    {
48
        $env->put(self::TRAEFIK_UI_PASSWORD, $value);
49
    }
50
51
    public static function optionName(): string
52
    {
53
        return self::OPTION_NAME;
54
    }
55
56
    public function toString(): string
57
    {
58
        return $this->value;
59
    }
60
61
    public function __toString()
62
    {
63
        return $this->toString();
64
    }
65
}