TraefikHostname   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A toHostname() 0 3 1
A domainName() 0 3 1
A toLocal() 0 3 1
A __toString() 0 3 1
A __construct() 0 4 1
A toString() 0 3 1
A optionName() 0 3 1
A toEnv() 0 3 1
A fromEnv() 0 3 1
A toHostnameCollection() 0 3 1
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
}