Completed
Push — master ( aa77c9...15a70d )
by Alejandro
27s
created

setImportedInstallationPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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 2
        ], $pathCollection));
142
143 2
        $this->setUrlShortener($this->mapExistingPathsToKeys([
144 2
            UrlShortenerConfigCustomizer::SCHEMA => ['url_shortener', 'domain', 'schema'],
145
            UrlShortenerConfigCustomizer::HOSTNAME => ['url_shortener', 'domain', 'hostname'],
146
            UrlShortenerConfigCustomizer::CHARS => ['url_shortener', 'shortcode_chars'],
147
            UrlShortenerConfigCustomizer::VALIDATE_URL => ['url_shortener', 'validate_url'],
148
            UrlShortenerConfigCustomizer::ENABLE_NOT_FOUND_REDIRECTION => [
149
                'url_shortener',
150
                'not_found_short_url',
151
                'enable_redirection',
152
            ],
153
            UrlShortenerConfigCustomizer::NOT_FOUND_REDIRECT_TO => [
154
                'url_shortener',
155
                'not_found_short_url',
156
                'redirect_to',
157
            ],
158 2
        ], $pathCollection));
159
    }
160
161 2
    private function mapExistingPathsToKeys(array $map, PathCollection $pathCollection): array
162
    {
163 2
        $result = [];
164 2
        foreach ($map as $key => $path) {
165 2
            if ($pathCollection->pathExists($path)) {
166 2
                $result[$key] = $pathCollection->getValueInPath($path);
167
            }
168
        }
169
170 2
        return $result;
171
    }
172
173 3
    public function getArrayCopy(): array
174
    {
175 3
        $dbDriver = $this->database[DatabaseConfigCustomizer::DRIVER] ?? '';
176
        $config = [
177 3
            'app_options' => [
178 3
                'secret_key' => $this->app[ApplicationConfigCustomizer::SECRET] ?? '',
179 3
                'disable_track_param' => $this->app[ApplicationConfigCustomizer::DISABLE_TRACK_PARAM] ?? null,
180
            ],
181
            'delete_short_urls' => [
182 3
                'check_visits_threshold' => $this->app[ApplicationConfigCustomizer::CHECK_VISITS_THRESHOLD] ?? true,
183 3
                'visits_threshold' => $this->app[ApplicationConfigCustomizer::VISITS_THRESHOLD] ?? 15,
184
            ],
185
            'entity_manager' => [
186
                'connection' => [
187 3
                    'driver' => $dbDriver,
188
                ],
189
            ],
190
            'translator' => [
191 3
                'locale' => $this->language[LanguageConfigCustomizer::DEFAULT_LANG] ?? 'en',
192
            ],
193
            'url_shortener' => [
194
                'domain' => [
195 3
                    'schema' => $this->urlShortener[UrlShortenerConfigCustomizer::SCHEMA] ?? 'http',
196 3
                    'hostname' => $this->urlShortener[UrlShortenerConfigCustomizer::HOSTNAME] ?? '',
197
                ],
198 3
                'shortcode_chars' => $this->urlShortener[UrlShortenerConfigCustomizer::CHARS] ?? '',
199 3
                'validate_url' => $this->urlShortener[UrlShortenerConfigCustomizer::VALIDATE_URL] ?? true,
200
                'not_found_short_url' => [
201
                    'enable_redirection' =>
202 3
                        $this->urlShortener[UrlShortenerConfigCustomizer::ENABLE_NOT_FOUND_REDIRECTION] ?? false,
203 3
                    'redirect_to' => $this->urlShortener[UrlShortenerConfigCustomizer::NOT_FOUND_REDIRECT_TO] ?? null,
204
                ],
205
            ],
206
        ];
207
208
        // Build dynamic database config based on selected driver
209 3
        if ($dbDriver === 'pdo_sqlite') {
210
            $config['entity_manager']['connection']['path'] = self::SQLITE_DB_PATH;
211
        } else {
212 3
            $config['entity_manager']['connection']['user'] = $this->database[DatabaseConfigCustomizer::USER] ?? '';
213 3
            $config['entity_manager']['connection']['password'] =
214 3
                $this->database[DatabaseConfigCustomizer::PASSWORD] ?? '';
215 3
            $config['entity_manager']['connection']['dbname'] = $this->database[DatabaseConfigCustomizer::NAME] ?? '';
216 3
            $config['entity_manager']['connection']['host'] = $this->database[DatabaseConfigCustomizer::HOST] ?? '';
217 3
            $config['entity_manager']['connection']['port'] = $this->database[DatabaseConfigCustomizer::PORT] ?? '';
218
219 3
            if ($dbDriver === 'pdo_mysql') {
220
                $config['entity_manager']['connection']['driverOptions'] = [
221
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
222
                ];
223
            }
224
        }
225
226 3
        return $config;
227
    }
228
}
229