ProvisionConfig   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Importance

Changes 0
Metric Value
wmc 18
lcom 4
cbo 3
dl 0
loc 184
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A withEnvPath() 0 6 1
A force() 0 6 1
A skipProvisioned() 0 6 1
A skipSyntaxCheck() 0 6 1
A sendEmail() 0 6 1
A skipEmail() 0 6 1
A load() 0 9 2
A getEnvPath() 0 4 1
A isForce() 0 4 1
A isSkipProvisioned() 0 4 1
A isSkipSyntaxCheck() 0 4 1
A isSendEmail() 0 4 1
A composeDotenv() 0 20 4
1
<?php
2
3
namespace Tworzenieweb\SqlProvisioner\Config;
4
5
use Dotenv\Dotenv;
6
7
/**
8
 * Class ProvisionConfig
9
 *
10
 * @package Tworzenieweb\SqlProvisioner\Config
11
 */
12
class ProvisionConfig
13
{
14
    /**
15
     * @var string
16
     */
17
    private $envPath;
18
19
    /**
20
     * @var bool
21
     */
22
    private $force = false;
23
24
    /**
25
     * @var bool
26
     */
27
    private $skipProvisioned = false;
28
29
    /**
30
     * @var bool
31
     */
32
    private $skipSyntaxCheck = false;
33
34
    /**
35
     * @var EmailConfig
36
     */
37
    private $emailConfig;
38
39
    /**
40
     * ProvisionConfig constructor.
41
     *
42
     * @param string      $envPath
43
     * @param EmailConfig $emailConfig
44
     */
45
    public function __construct(string $envPath, EmailConfig $emailConfig)
46
    {
47
        $this->envPath     = $envPath;
48
        $this->emailConfig = $emailConfig;
49
    }
50
51
    /**
52
     * @param string $envPath
53
     *
54
     * @return ProvisionConfig
55
     */
56
    public function withEnvPath(string $envPath): ProvisionConfig
57
    {
58
        $this->envPath = $envPath;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param bool $force
65
     *
66
     * @return ProvisionConfig
67
     */
68
    public function force(bool $force = true): ProvisionConfig
69
    {
70
        $this->force = $force;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param bool $skipProvisioned
77
     *
78
     * @return ProvisionConfig
79
     */
80
    public function skipProvisioned(bool $skipProvisioned = true): ProvisionConfig
81
    {
82
        $this->skipProvisioned = $skipProvisioned;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param bool $skipSyntaxCheck
89
     *
90
     * @return ProvisionConfig
91
     */
92
    public function skipSyntaxCheck(bool $skipSyntaxCheck = true): ProvisionConfig
93
    {
94
        $this->skipSyntaxCheck = $skipSyntaxCheck;
95
96
        return $this;
97
    }
98
99
    /**
100
     * @return ProvisionConfig
101
     */
102
    public function sendEmail(): ProvisionConfig
103
    {
104
        $this->emailConfig->enable();
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return ProvisionConfig
111
     */
112
    public function skipEmail(): ProvisionConfig
113
    {
114
        $this->emailConfig->disable();
115
116
        return $this;
117
    }
118
119
    /**
120
     * @return void
121
     */
122
    public function load()
123
    {
124
        $loader = $this->composeDotenv();
125
        $loader->load();
126
127
        if ($this->emailConfig->isEnabled()) {
128
            $loader->required(EmailConfig::MANDATORY_ENV_VARIABLES)->notEmpty();
129
        }
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getEnvPath(): string
136
    {
137
        return $this->envPath;
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function isForce(): bool
144
    {
145
        return $this->force;
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function isSkipProvisioned(): bool
152
    {
153
        return $this->skipProvisioned;
154
    }
155
156
    /**
157
     * @return bool
158
     */
159
    public function isSkipSyntaxCheck(): bool
160
    {
161
        return $this->skipSyntaxCheck;
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function isSendEmail(): bool
168
    {
169
        return $this->sendEmail;
0 ignored issues
show
Bug introduced by
The property sendEmail does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
170
    }
171
172
    /**
173
     * @return Dotenv
174
     */
175
    protected function composeDotenv(): Dotenv
176
    {
177
        if (!is_readable($this->envPath)) {
178
            throw new \RuntimeException(
179
                sprintf('Env file path [%s] is not readable', $this->envPath)
180
            );
181
        }
182
183
        if (is_dir($this->envPath)) {
184
            return new Dotenv($this->envPath);
185
        }
186
187
        if (is_file($this->envPath)) {
188
            return new Dotenv(dirname($this->envPath), basename($this->envPath));
189
        }
190
191
        throw new \RuntimeException(
192
            sprintf('Could not define whether provided env path [%s] is a directory or file', $this->envPath)
193
        );
194
    }
195
}
196