TraefikHostname::toEnv()   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 2
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\DigitalOcean\Hostname;
7
use Cocotte\DigitalOcean\HostnameCollection;
8
use Cocotte\Environment\FromEnvLazyFactory;
9
use Cocotte\Environment\LazyEnvironmentValue;
10
use Cocotte\Environment\LazyExportableOption;
11
use Cocotte\Shell\Env;
12
13
class TraefikHostname implements LazyExportableOption, FromEnvLazyFactory
14
{
15
    const TRAEFIK_UI_HOSTNAME = 'TRAEFIK_UI_HOSTNAME';
16
    const OPTION_NAME = 'traefik-ui-hostname';
17
    const REGEX = '/^[a-zA-Z0-9_@#%?&*+=!-]+$/';
18
19
    /**
20
     * @var Hostname
21
     */
22
    private $hostname;
23
24
    public function __construct(Hostname $hostname)
25
    {
26
        Assertion::false($hostname->isRoot(), "$hostname does not have a subdomain.");
27
        $this->hostname = $hostname;
28
    }
29
30
    /**
31
     * @param Env $env
32
     * @return LazyEnvironmentValue|self
33
     * @throws \Exception
34
     */
35
    public static function fromEnv(Env $env): LazyEnvironmentValue
36
    {
37
        return new self(Hostname::parse($env->get(self::TRAEFIK_UI_HOSTNAME, "")));
0 ignored issues
show
Bug introduced by
It seems like $env->get(self::TRAEFIK_UI_HOSTNAME, '') can also be of type null; however, parameter $value of Cocotte\DigitalOcean\Hostname::parse() 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

37
        return new self(Hostname::parse(/** @scrutinizer ignore-type */ $env->get(self::TRAEFIK_UI_HOSTNAME, "")));
Loading history...
38
    }
39
40
    public static function toEnv(string $value, Env $env): void
41
    {
42
        $env->put(self::TRAEFIK_UI_HOSTNAME, $value);
43
    }
44
45
    public static function optionName(): string
46
    {
47
        return self::OPTION_NAME;
48
    }
49
50
    public function __toString()
51
    {
52
        return $this->toString();
53
    }
54
55
    public function toString(): string
56
    {
57
        return $this->hostname->toString();
58
    }
59
60
    public function toHostnameCollection(): HostnameCollection
61
    {
62
        return new HostnameCollection($this->hostname);
63
    }
64
65
    public function toLocal(): Hostname
66
    {
67
        return $this->hostname->toLocal();
68
    }
69
70
    public function domainName()
71
    {
72
        return $this->hostname->domainName();
73
    }
74
75
    public function toHostname(): Hostname
76
    {
77
        return $this->hostname;
78
    }
79
}