TraefikUsername::optionName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 TraefikUsername implements LazyExportableOption, FromEnvLazyFactory
12
{
13
    const SUGGESTED_VALUE = 'admin';
14
    const TRAEFIK_UI_USERNAME = 'TRAEFIK_UI_USERNAME';
15
    const OPTION_NAME = 'traefik-ui-username';
16
    const REGEX = '/^[a-zA-Z0-9]+$/';
17
    /**
18
     * @var string
19
     */
20
    private $value;
21
22
    public function __construct(string $value)
23
    {
24
        Assertion::notEmpty($value, "The Traefik Ui username is empty");
25
        Assertion::regex(
26
            $value,
27
            self::REGEX,
28
            "The Traefik Ui username does not contain only alphanumeric characters."
29
        );
30
        $this->value = $value;
31
    }
32
33
    public static function fromString(string $value): self
34
    {
35
        return new self($value);
36
    }
37
38
    /**
39
     * @param Env $env
40
     * @return LazyEnvironmentValue|self
41
     */
42
    public static function fromEnv(Env $env): LazyEnvironmentValue
43
    {
44
        return new self($env->get(self::TRAEFIK_UI_USERNAME, ""));
0 ignored issues
show
Bug introduced by
It seems like $env->get(self::TRAEFIK_UI_USERNAME, '') can also be of type null; however, parameter $value of Cocotte\Template\Traefik...Username::__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

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