Completed
Push — master ( aca90e...8ef0e7 )
by Alejandro
10s
created

CustomizableAppConfig   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
dl 0
loc 263
rs 10
c 0
b 0
f 0
ccs 58
cts 87
cp 0.6667
wmc 26
lcom 2
cbo 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getDatabase() 0 4 1
A setDatabase() 0 5 1
A hasDatabase() 0 4 1
A getUrlShortener() 0 4 1
A setUrlShortener() 0 5 1
A hasUrlShortener() 0 4 1
A getLanguage() 0 4 1
A setLanguage() 0 5 1
A hasLanguage() 0 4 1
A getApp() 0 4 1
A setApp() 0 5 1
A hasApp() 0 4 1
A getImportedInstallationPath() 0 4 1
A setImportedInstallationPath() 0 5 1
A hasImportedInstallationPath() 0 4 1
B exchangeArray() 0 29 5
A deserializeDatabase() 0 18 3
A getArrayCopy() 0 47 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\CLI\Model;
5
6
use Zend\Stdlib\ArraySerializableInterface;
7
8
final class CustomizableAppConfig implements ArraySerializableInterface
9
{
10
    const SQLITE_DB_PATH = 'data/database.sqlite';
11
12
    /**
13
     * @var array
14
     */
15
    private $database;
16
    /**
17
     * @var array
18
     */
19
    private $urlShortener;
20
    /**
21
     * @var array
22
     */
23
    private $language;
24
    /**
25
     * @var array
26
     */
27
    private $app;
28
    /**
29
     * @var string
30
     */
31
    private $importedInstallationPath;
32
33
    /**
34
     * @return array
35
     */
36 4
    public function getDatabase()
37
    {
38 4
        return $this->database;
39
    }
40
41
    /**
42
     * @param array $database
43
     * @return $this
44
     */
45 4
    public function setDatabase(array $database)
46
    {
47 4
        $this->database = $database;
48 4
        return $this;
49
    }
50
51
    /**
52
     * @return bool
53
     */
54 4
    public function hasDatabase()
55
    {
56 4
        return ! empty($this->database);
57
    }
58
59
    /**
60
     * @return array
61
     */
62 3
    public function getUrlShortener()
63
    {
64 3
        return $this->urlShortener;
65
    }
66
67
    /**
68
     * @param array $urlShortener
69
     * @return $this
70
     */
71 3
    public function setUrlShortener(array $urlShortener)
72
    {
73 3
        $this->urlShortener = $urlShortener;
74 3
        return $this;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 3
    public function hasUrlShortener()
81
    {
82 3
        return ! empty($this->urlShortener);
83
    }
84
85
    /**
86
     * @return array
87
     */
88 3
    public function getLanguage()
89
    {
90 3
        return $this->language;
91
    }
92
93
    /**
94
     * @param array $language
95
     * @return $this
96
     */
97 3
    public function setLanguage(array $language)
98
    {
99 3
        $this->language = $language;
100 3
        return $this;
101
    }
102
103
    /**
104
     * @return bool
105
     */
106 3
    public function hasLanguage()
107
    {
108 3
        return ! empty($this->language);
109
    }
110
111
    /**
112
     * @return array
113
     */
114 3
    public function getApp()
115
    {
116 3
        return $this->app;
117
    }
118
119
    /**
120
     * @param array $app
121
     * @return $this
122
     */
123 3
    public function setApp(array $app)
124
    {
125 3
        $this->app = $app;
126 3
        return $this;
127
    }
128
129
    /**
130
     * @return bool
131
     */
132 3
    public function hasApp()
133
    {
134 3
        return ! empty($this->app);
135
    }
136
137
    /**
138
     * @return string
139
     */
140 2
    public function getImportedInstallationPath()
141
    {
142 2
        return $this->importedInstallationPath;
143
    }
144
145
    /**
146
     * @param string $importedInstallationPath
147
     * @return $this|self
148
     */
149 1
    public function setImportedInstallationPath($importedInstallationPath)
150
    {
151 1
        $this->importedInstallationPath = $importedInstallationPath;
152 1
        return $this;
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function hasImportedInstallationPath()
159
    {
160
        return $this->importedInstallationPath !== null;
161
    }
162
163
    /**
164
     * Exchange internal values from provided array
165
     *
166
     * @param array $array
167
     * @return void
168
     */
169 1
    public function exchangeArray(array $array)
170
    {
171 1
        if (isset($array['app_options'], $array['app_options']['secret_key'])) {
172
            $this->setApp([
173
                'SECRET' => $array['app_options']['secret_key'],
174
            ]);
175
        }
176
177 1
        if (isset($array['entity_manager'], $array['entity_manager']['connection'])) {
178
            $this->deserializeDatabase($array['entity_manager']['connection']);
179
        }
180
181 1
        if (isset($array['translator'], $array['translator']['locale'], $array['cli'], $array['cli']['locale'])) {
182
            $this->setLanguage([
183
                'DEFAULT' => $array['translator']['locale'],
184
                'CLI' => $array['cli']['locale'],
185
            ]);
186
        }
187
188 1
        if (isset($array['url_shortener'])) {
189
            $urlShortener = $array['url_shortener'];
190
            $this->setUrlShortener([
191
                'SCHEMA' => $urlShortener['domain']['schema'],
192
                'HOSTNAME' => $urlShortener['domain']['hostname'],
193
                'CHARS' => $urlShortener['shortcode_chars'],
194
                'VALIDATE_URL' => $urlShortener['validate_url'] ?? true,
195
            ]);
196
        }
197 1
    }
198
199
    private function deserializeDatabase(array $conn)
200
    {
201
        if (! isset($conn['driver'])) {
202
            return;
203
        }
204
        $driver = $conn['driver'];
205
206
        $params = ['DRIVER' => $driver];
207
        if ($driver !== 'pdo_sqlite') {
208
            $params['USER'] = $conn['user'];
209
            $params['PASSWORD'] = $conn['password'];
210
            $params['NAME'] = $conn['dbname'];
211
            $params['HOST'] = $conn['host'];
212
            $params['PORT'] = $conn['port'];
213
        }
214
215
        $this->setDatabase($params);
216
    }
217
218
    /**
219
     * Return an array representation of the object
220
     *
221
     * @return array
222
     */
223 3
    public function getArrayCopy()
224
    {
225
        $config = [
226 3
            'app_options' => [
227 3
                'secret_key' => $this->app['SECRET'],
228 3
                'disable_track_param' => $this->app['DISABLE_TRACK_PARAM'] ?? null,
229
            ],
230
            'entity_manager' => [
231
                'connection' => [
232 3
                    'driver' => $this->database['DRIVER'],
233
                ],
234
            ],
235
            'translator' => [
236 3
                'locale' => $this->language['DEFAULT'],
237
            ],
238
            'cli' => [
239 3
                'locale' => $this->language['CLI'],
240
            ],
241
            'url_shortener' => [
242
                'domain' => [
243 3
                    'schema' => $this->urlShortener['SCHEMA'],
244 3
                    'hostname' => $this->urlShortener['HOSTNAME'],
245
                ],
246 3
                'shortcode_chars' => $this->urlShortener['CHARS'],
247 3
                'validate_url' => $this->urlShortener['VALIDATE_URL'],
248
            ],
249
        ];
250
251
        // Build dynamic database config based on selected driver
252 3
        if ($this->database['DRIVER'] === 'pdo_sqlite') {
253
            $config['entity_manager']['connection']['path'] = self::SQLITE_DB_PATH;
254
        } else {
255 3
            $config['entity_manager']['connection']['user'] = $this->database['USER'];
256 3
            $config['entity_manager']['connection']['password'] = $this->database['PASSWORD'];
257 3
            $config['entity_manager']['connection']['dbname'] = $this->database['NAME'];
258 3
            $config['entity_manager']['connection']['host'] = $this->database['HOST'];
259 3
            $config['entity_manager']['connection']['port'] = $this->database['PORT'];
260
261 3
            if ($this->database['DRIVER'] === 'pdo_mysql') {
262
                $config['entity_manager']['connection']['driverOptions'] = [
263
                    \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
264
                ];
265
            }
266
        }
267
268 3
        return $config;
269
    }
270
}
271