Completed
Pull Request — master (#340)
by Alejandro
05:57
created

CustomizableAppConfig::getArrayCopy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 3.004

Importance

Changes 0
Metric Value
cc 3
eloc 36
nc 3
nop 0
dl 0
loc 54
ccs 24
cts 26
cp 0.9231
crap 3.004
rs 9.344
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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