Completed
Pull Request — master (#252)
by Alejandro
03:36 queued 01:06
created

CustomizableAppConfig   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
eloc 103
dl 0
loc 217
ccs 78
cts 82
cp 0.9512
rs 10
c 0
b 0
f 0
wmc 22

18 Methods

Rating   Name   Duplication   Size   Complexity  
A setImportedInstallationPath() 0 4 1
A getUrlShortener() 0 3 1
A hasUrlShortener() 0 3 1
A setDatabase() 0 4 1
A hasDatabase() 0 3 1
A hasApp() 0 3 1
A getImportedInstallationPath() 0 3 1
A getLanguage() 0 3 1
A getDatabase() 0 3 1
A setLanguage() 0 4 1
A setUrlShortener() 0 4 1
A setApp() 0 4 1
A hasLanguage() 0 3 1
A hasImportedInstallationPath() 0 3 1
A getApp() 0 3 1
A getArrayCopy() 0 57 3
A exchangeArray() 0 41 1
A mapExistingPathsToKeys() 0 10 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Installer\Model;
5
6
use PDO;
7
use Shlinkio\Shlink\Common\Collection\PathCollection;
8
use Shlinkio\Shlink\Installer\Config\Plugin\ApplicationConfigCustomizer;
9
use Shlinkio\Shlink\Installer\Config\Plugin\DatabaseConfigCustomizer;
10
use Shlinkio\Shlink\Installer\Config\Plugin\LanguageConfigCustomizer;
11
use Shlinkio\Shlink\Installer\Config\Plugin\UrlShortenerConfigCustomizer;
12
use Zend\Stdlib\ArraySerializableInterface;
13
14
final class CustomizableAppConfig implements ArraySerializableInterface
15
{
16
    public const SQLITE_DB_PATH = 'data/database.sqlite';
17
18
    /**
19
     * @var array
20
     */
21
    private $database = [];
22
    /**
23
     * @var array
24
     */
25
    private $urlShortener = [];
26
    /**
27
     * @var array
28
     */
29
    private $language = [];
30
    /**
31
     * @var array
32
     */
33
    private $app = [];
34
    /**
35
     * @var string|null
36
     */
37
    private $importedInstallationPath;
38
39 4
    public function getDatabase(): array
40
    {
41 4
        return $this->database;
42
    }
43
44 6
    public function setDatabase(array $database): self
45
    {
46 6
        $this->database = $database;
47 6
        return $this;
48
    }
49
50 5
    public function hasDatabase(): bool
51
    {
52 5
        return ! empty($this->database);
53
    }
54
55 4
    public function getUrlShortener(): array
56
    {
57 4
        return $this->urlShortener;
58
    }
59
60 6
    public function setUrlShortener(array $urlShortener): self
61
    {
62 6
        $this->urlShortener = $urlShortener;
63 6
        return $this;
64
    }
65
66 5
    public function hasUrlShortener(): bool
67
    {
68 5
        return ! empty($this->urlShortener);
69
    }
70
71 4
    public function getLanguage(): array
72
    {
73 4
        return $this->language;
74
    }
75
76 5
    public function setLanguage(array $language): self
77
    {
78 5
        $this->language = $language;
79 5
        return $this;
80
    }
81
82 4
    public function hasLanguage(): bool
83
    {
84 4
        return ! empty($this->language);
85
    }
86
87 5
    public function getApp(): array
88
    {
89 5
        return $this->app;
90
    }
91
92 6
    public function setApp(array $app): self
93
    {
94 6
        $this->app = $app;
95 6
        return $this;
96
    }
97
98 5
    public function hasApp(): bool
99
    {
100 5
        return ! empty($this->app);
101
    }
102
103 2
    public function getImportedInstallationPath(): ?string
104
    {
105 2
        return $this->importedInstallationPath;
106
    }
107
108 1
    public function setImportedInstallationPath(string $importedInstallationPath): self
109
    {
110 1
        $this->importedInstallationPath = $importedInstallationPath;
111 1
        return $this;
112
    }
113
114
    public function hasImportedInstallationPath(): bool
115
    {
116
        return $this->importedInstallationPath !== null;
117
    }
118
119 2
    public function exchangeArray(array $array): void
120
    {
121 2
        $pathCollection = new PathCollection($array);
122
123 2
        $this->setApp($this->mapExistingPathsToKeys([
124 2
            ApplicationConfigCustomizer::SECRET => ['app_options', 'secret_key'],
125
            ApplicationConfigCustomizer::DISABLE_TRACK_PARAM => ['app_options', 'disable_track_param'],
126
            ApplicationConfigCustomizer::CHECK_VISITS_THRESHOLD => ['delete_short_urls', 'check_visits_threshold'],
127
            ApplicationConfigCustomizer::VISITS_THRESHOLD => ['delete_short_urls', 'visits_threshold'],
128 2
        ], $pathCollection));
129
130 2
        $this->setDatabase($this->mapExistingPathsToKeys([
131 2
            DatabaseConfigCustomizer::DRIVER => ['entity_manager', 'connection', 'driver'],
132
            DatabaseConfigCustomizer::USER => ['entity_manager', 'connection', 'user'],
133
            DatabaseConfigCustomizer::PASSWORD => ['entity_manager', 'connection', 'password'],
134
            DatabaseConfigCustomizer::NAME => ['entity_manager', 'connection', 'dbname'],
135
            DatabaseConfigCustomizer::HOST => ['entity_manager', 'connection', 'host'],
136
            DatabaseConfigCustomizer::PORT => ['entity_manager', 'connection', 'port'],
137 2
        ], $pathCollection));
138
139 2
        $this->setLanguage($this->mapExistingPathsToKeys([
140 2
            LanguageConfigCustomizer::DEFAULT_LANG => ['translator', 'locale'],
141
            LanguageConfigCustomizer::CLI_LANG => ['cli', 'locale'],
142 2
        ], $pathCollection));
143
144 2
        $this->setUrlShortener($this->mapExistingPathsToKeys([
145 2
            UrlShortenerConfigCustomizer::SCHEMA => ['url_shortener', 'domain', 'schema'],
146
            UrlShortenerConfigCustomizer::HOSTNAME => ['url_shortener', 'domain', 'hostname'],
147
            UrlShortenerConfigCustomizer::CHARS => ['url_shortener', 'shortcode_chars'],
148
            UrlShortenerConfigCustomizer::VALIDATE_URL => ['url_shortener', 'validate_url'],
149
            UrlShortenerConfigCustomizer::ENABLE_NOT_FOUND_REDIRECTION => [
150
                'url_shortener',
151
                'not_found_short_url',
152
                'enable_redirection',
153
            ],
154
            UrlShortenerConfigCustomizer::NOT_FOUND_REDIRECT_TO => [
155
                'url_shortener',
156
                'not_found_short_url',
157
                'redirect_to',
158
            ],
159 2
        ], $pathCollection));
160
    }
161
162 2
    private function mapExistingPathsToKeys(array $map, PathCollection $pathCollection): array
163
    {
164 2
        $result = [];
165 2
        foreach ($map as $key => $path) {
166 2
            if ($pathCollection->pathExists($path)) {
167 2
                $result[$key] = $pathCollection->getValueInPath($path);
168
            }
169
        }
170
171 2
        return $result;
172
    }
173
174 3
    public function getArrayCopy(): array
175
    {
176 3
        $dbDriver = $this->database[DatabaseConfigCustomizer::DRIVER] ?? '';
177
        $config = [
178 3
            'app_options' => [
179 3
                'secret_key' => $this->app[ApplicationConfigCustomizer::SECRET] ?? '',
180 3
                'disable_track_param' => $this->app[ApplicationConfigCustomizer::DISABLE_TRACK_PARAM] ?? null,
181
            ],
182
            'delete_short_urls' => [
183 3
                'check_visits_threshold' => $this->app[ApplicationConfigCustomizer::CHECK_VISITS_THRESHOLD] ?? true,
184 3
                'visits_threshold' => $this->app[ApplicationConfigCustomizer::VISITS_THRESHOLD] ?? 15,
185
            ],
186
            'entity_manager' => [
187
                'connection' => [
188 3
                    'driver' => $dbDriver,
189
                ],
190
            ],
191
            'translator' => [
192 3
                'locale' => $this->language[LanguageConfigCustomizer::DEFAULT_LANG] ?? 'en',
193
            ],
194
            'cli' => [
195 3
                'locale' => $this->language[LanguageConfigCustomizer::CLI_LANG] ?? 'en',
196
            ],
197
            'url_shortener' => [
198
                'domain' => [
199 3
                    'schema' => $this->urlShortener[UrlShortenerConfigCustomizer::SCHEMA] ?? 'http',
200 3
                    'hostname' => $this->urlShortener[UrlShortenerConfigCustomizer::HOSTNAME] ?? '',
201
                ],
202 3
                'shortcode_chars' => $this->urlShortener[UrlShortenerConfigCustomizer::CHARS] ?? '',
203 3
                'validate_url' => $this->urlShortener[UrlShortenerConfigCustomizer::VALIDATE_URL] ?? true,
204
                'not_found_short_url' => [
205
                    'enable_redirection' =>
206 3
                        $this->urlShortener[UrlShortenerConfigCustomizer::ENABLE_NOT_FOUND_REDIRECTION] ?? false,
207 3
                    'redirect_to' => $this->urlShortener[UrlShortenerConfigCustomizer::NOT_FOUND_REDIRECT_TO] ?? null,
208
                ],
209
            ],
210
        ];
211
212
        // Build dynamic database config based on selected driver
213 3
        if ($dbDriver === 'pdo_sqlite') {
214
            $config['entity_manager']['connection']['path'] = self::SQLITE_DB_PATH;
215
        } else {
216 3
            $config['entity_manager']['connection']['user'] = $this->database[DatabaseConfigCustomizer::USER] ?? '';
217 3
            $config['entity_manager']['connection']['password'] =
218 3
                $this->database[DatabaseConfigCustomizer::PASSWORD] ?? '';
219 3
            $config['entity_manager']['connection']['dbname'] = $this->database[DatabaseConfigCustomizer::NAME] ?? '';
220 3
            $config['entity_manager']['connection']['host'] = $this->database[DatabaseConfigCustomizer::HOST] ?? '';
221 3
            $config['entity_manager']['connection']['port'] = $this->database[DatabaseConfigCustomizer::PORT] ?? '';
222
223 3
            if ($dbDriver === 'pdo_mysql') {
224
                $config['entity_manager']['connection']['driverOptions'] = [
225
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
226
                ];
227
            }
228
        }
229
230 3
        return $config;
231
    }
232
}
233