Passed
Push — master ( 956163...dedbc7 )
by Christian
03:02
created

StaticSiteHostname   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toHostname() 0 3 1
A fromEnv() 0 3 1
A __construct() 0 4 1
A toString() 0 3 1
A toEnv() 0 3 1
A toHostnameCollection() 0 3 1
A toLocal() 0 3 1
A __toString() 0 3 1
A optionName() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Template\StaticSite;
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 StaticSiteHostname implements LazyExportableOption, FromEnvLazyFactory
14
{
15
    const STATIC_SITE_HOSTNAME = 'STATIC_SITE_HOSTNAME';
16
    const OPTION_NAME = 'hostname';
17
18
    /**
19
     * @var Hostname
20
     */
21
    private $hostname;
22
23
    public function __construct(Hostname $hostname)
24
    {
25
        Assertion::false($hostname->isRoot(), "$hostname does not have a subdomain.");
26
        $this->hostname = $hostname;
27
    }
28
29
    /**
30
     * @param Env $env
31
     * @return LazyEnvironmentValue|self
32
     * @throws \Exception
33
     */
34
    public static function fromEnv(Env $env): LazyEnvironmentValue
35
    {
36
        return new self(Hostname::parse($env->get(self::STATIC_SITE_HOSTNAME, "")));
0 ignored issues
show
Bug introduced by
It seems like $env->get(self::STATIC_SITE_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

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