Completed
Pull Request — master (#227)
by Alejandro
03:26
created

CustomizableAppConfig::hasLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Installer\Model;
5
6
use Shlinkio\Shlink\Installer\Config\Plugin\ApplicationConfigCustomizer;
7
use Shlinkio\Shlink\Installer\Config\Plugin\DatabaseConfigCustomizer;
8
use Shlinkio\Shlink\Installer\Config\Plugin\LanguageConfigCustomizer;
9
use Shlinkio\Shlink\Installer\Config\Plugin\UrlShortenerConfigCustomizer;
10
use Zend\Stdlib\ArraySerializableInterface;
11
use function Shlinkio\Shlink\Common\array_get_path;
12
use function Shlinkio\Shlink\Common\array_path_exists;
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 3
    public function getUrlShortener(): array
56
    {
57 3
        return $this->urlShortener;
58
    }
59
60 5
    public function setUrlShortener(array $urlShortener): self
61
    {
62 5
        $this->urlShortener = $urlShortener;
63 5
        return $this;
64
    }
65
66 4
    public function hasUrlShortener(): bool
67
    {
68 4
        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
        $this->setApp($this->mapExistingPathsToKeys([
122 2
            ApplicationConfigCustomizer::SECRET => ['app_options', 'secret_key'],
123
            ApplicationConfigCustomizer::DISABLE_TRACK_PARAM => ['app_options', 'disable_track_param'],
124
            ApplicationConfigCustomizer::CHECK_VISITS_THRESHOLD => ['delete_short_urls', 'check_visits_threshold'],
125
            ApplicationConfigCustomizer::VISITS_THRESHOLD => ['delete_short_urls', 'visits_threshold'],
126 2
        ], $array));
127
128 2
        $this->setDatabase($this->mapExistingPathsToKeys([
129 2
            DatabaseConfigCustomizer::DRIVER => ['entity_manager', 'connection', 'driver'],
130
            DatabaseConfigCustomizer::USER => ['entity_manager', 'connection', 'user'],
131
            DatabaseConfigCustomizer::PASSWORD => ['entity_manager', 'connection', 'password'],
132
            DatabaseConfigCustomizer::NAME => ['entity_manager', 'connection', 'dbname'],
133
            DatabaseConfigCustomizer::HOST => ['entity_manager', 'connection', 'host'],
134
            DatabaseConfigCustomizer::PORT => ['entity_manager', 'connection', 'port'],
135 2
        ], $array));
136
137 2
        $this->setLanguage($this->mapExistingPathsToKeys([
138 2
            LanguageConfigCustomizer::DEFAULT_LANG => ['translator', 'locale'],
139
            LanguageConfigCustomizer::CLI_LANG => ['cli', 'locale'],
140 2
        ], $array));
141
142 2
        $this->setUrlShortener($this->mapExistingPathsToKeys([
143 2
            UrlShortenerConfigCustomizer::SCHEMA => ['url_shortener', 'domain', 'schema'],
144
            UrlShortenerConfigCustomizer::HOSTNAME => ['url_shortener', 'domain', 'hostname'],
145
            UrlShortenerConfigCustomizer::CHARS => ['url_shortener', 'shortcode_chars'],
146
            UrlShortenerConfigCustomizer::VALIDATE_URL => ['url_shortener', 'validate_url'],
147 2
        ], $array));
148 2
    }
149
150 2
    private function mapExistingPathsToKeys(array $map, array $config): array
151
    {
152 2
        $result = [];
153 2
        foreach ($map as $key => $path) {
154 2
            if (array_path_exists($path, $config)) {
155 2
                $result[$key] = array_get_path($path, $config);
156
            }
157
        }
158
159 2
        return $result;
160
    }
161
162 3
    public function getArrayCopy(): array
163
    {
164 3
        $dbDriver = $this->database[DatabaseConfigCustomizer::DRIVER] ?? '';
165
        $config = [
166 3
            'app_options' => [
167 3
                'secret_key' => $this->app[ApplicationConfigCustomizer::SECRET] ?? '',
168
                'disable_track_param' => $this->app[ApplicationConfigCustomizer::DISABLE_TRACK_PARAM] ?? null,
169
            ],
170
            'delete_short_urls' => [
171
                'check_visits_threshold' => $this->app[ApplicationConfigCustomizer::CHECK_VISITS_THRESHOLD] ?? true,
172 3
                'visits_threshold' => $this->app[ApplicationConfigCustomizer::VISITS_THRESHOLD] ?? 15,
173
            ],
174
            'entity_manager' => [
175
                'connection' => [
176 3
                    'driver' => $dbDriver,
177
                ],
178
            ],
179
            'translator' => [
180 3
                'locale' => $this->language[LanguageConfigCustomizer::DEFAULT_LANG] ?? 'en',
181
            ],
182
            'cli' => [
183 3
                'locale' => $this->language[LanguageConfigCustomizer::CLI_LANG] ?? 'en',
184
            ],
185
            'url_shortener' => [
186
                'domain' => [
187 3
                    'schema' => $this->urlShortener[UrlShortenerConfigCustomizer::SCHEMA] ?? 'http',
188 3
                    'hostname' => $this->urlShortener[UrlShortenerConfigCustomizer::HOSTNAME] ?? '',
189
                ],
190 3
                'shortcode_chars' => $this->urlShortener[UrlShortenerConfigCustomizer::CHARS] ?? '',
191
                'validate_url' => $this->urlShortener[UrlShortenerConfigCustomizer::VALIDATE_URL] ?? true,
192
            ],
193
        ];
194
195
        // Build dynamic database config based on selected driver
196 3
        if ($dbDriver === 'pdo_sqlite') {
197
            $config['entity_manager']['connection']['path'] = self::SQLITE_DB_PATH;
198
        } else {
199 3
            $config['entity_manager']['connection']['user'] = $this->database[DatabaseConfigCustomizer::USER] ?? '';
200 3
            $config['entity_manager']['connection']['password'] =
201 3
                $this->database[DatabaseConfigCustomizer::PASSWORD] ?? '';
202 3
            $config['entity_manager']['connection']['dbname'] = $this->database[DatabaseConfigCustomizer::NAME] ?? '';
203 3
            $config['entity_manager']['connection']['host'] = $this->database[DatabaseConfigCustomizer::HOST] ?? '';
204 3
            $config['entity_manager']['connection']['port'] = $this->database[DatabaseConfigCustomizer::PORT] ?? '';
205
206 3
            if ($dbDriver === 'pdo_mysql') {
207
                $config['entity_manager']['connection']['driverOptions'] = [
208
                    \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
209
                ];
210
            }
211
        }
212
213 3
        return $config;
214
    }
215
}
216