Completed
Pull Request — master (#220)
by Alejandro
03:52
created

getImportedInstallationPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Installer\Model;
5
6
use Zend\Stdlib\ArraySerializableInterface;
7
8
final class CustomizableAppConfig implements ArraySerializableInterface
9
{
10
    public 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
    public function getDatabase()
37
    {
38
        return $this->database;
39
    }
40
41
    /**
42
     * @param array $database
43
     * @return $this
44
     */
45
    public function setDatabase(array $database)
46
    {
47
        $this->database = $database;
48
        return $this;
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    public function hasDatabase()
55
    {
56
        return ! empty($this->database);
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getUrlShortener()
63
    {
64
        return $this->urlShortener;
65
    }
66
67
    /**
68
     * @param array $urlShortener
69
     * @return $this
70
     */
71
    public function setUrlShortener(array $urlShortener)
72
    {
73
        $this->urlShortener = $urlShortener;
74
        return $this;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function hasUrlShortener()
81
    {
82
        return ! empty($this->urlShortener);
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getLanguage()
89
    {
90
        return $this->language;
91
    }
92
93
    /**
94
     * @param array $language
95
     * @return $this
96
     */
97
    public function setLanguage(array $language)
98
    {
99
        $this->language = $language;
100
        return $this;
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    public function hasLanguage()
107
    {
108
        return ! empty($this->language);
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function getApp()
115
    {
116
        return $this->app;
117
    }
118
119
    /**
120
     * @param array $app
121
     * @return $this
122
     */
123
    public function setApp(array $app)
124
    {
125
        $this->app = $app;
126
        return $this;
127
    }
128
129
    /**
130
     * @return bool
131
     */
132
    public function hasApp()
133
    {
134
        return ! empty($this->app);
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getImportedInstallationPath()
141
    {
142
        return $this->importedInstallationPath;
143
    }
144
145
    /**
146
     * @param string $importedInstallationPath
147
     * @return $this|self
148
     */
149
    public function setImportedInstallationPath($importedInstallationPath)
150
    {
151
        $this->importedInstallationPath = $importedInstallationPath;
152
        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
    public function exchangeArray(array $array)
170
    {
171
        if (isset($array['app_options'], $array['app_options']['secret_key'])) {
172
            $this->setApp([
173
                'SECRET' => $array['app_options']['secret_key'],
174
            ]);
175
        }
176
177
        if (isset($array['entity_manager'], $array['entity_manager']['connection'])) {
178
            $this->deserializeDatabase($array['entity_manager']['connection']);
179
        }
180
181
        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
        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
    }
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
    public function getArrayCopy()
224
    {
225
        $config = [
226
            'app_options' => [
227
                'secret_key' => $this->app['SECRET'],
228
                'disable_track_param' => $this->app['DISABLE_TRACK_PARAM'] ?? null,
229
            ],
230
            'entity_manager' => [
231
                'connection' => [
232
                    'driver' => $this->database['DRIVER'],
233
                ],
234
            ],
235
            'translator' => [
236
                'locale' => $this->language['DEFAULT'],
237
            ],
238
            'cli' => [
239
                'locale' => $this->language['CLI'],
240
            ],
241
            'url_shortener' => [
242
                'domain' => [
243
                    'schema' => $this->urlShortener['SCHEMA'],
244
                    'hostname' => $this->urlShortener['HOSTNAME'],
245
                ],
246
                'shortcode_chars' => $this->urlShortener['CHARS'],
247
                'validate_url' => $this->urlShortener['VALIDATE_URL'],
248
            ],
249
        ];
250
251
        // Build dynamic database config based on selected driver
252
        if ($this->database['DRIVER'] === 'pdo_sqlite') {
253
            $config['entity_manager']['connection']['path'] = self::SQLITE_DB_PATH;
254
        } else {
255
            $config['entity_manager']['connection']['user'] = $this->database['USER'];
256
            $config['entity_manager']['connection']['password'] = $this->database['PASSWORD'];
257
            $config['entity_manager']['connection']['dbname'] = $this->database['NAME'];
258
            $config['entity_manager']['connection']['host'] = $this->database['HOST'];
259
            $config['entity_manager']['connection']['port'] = $this->database['PORT'];
260
261
            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
        return $config;
269
    }
270
}
271