Completed
Pull Request — master (#242)
by Alejandro
01:36
created

getImportedInstallationPath()   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\Common\Collection\PathCollection;
7
use Shlinkio\Shlink\Installer\Config\Plugin\ApplicationConfigCustomizer;
8
use Shlinkio\Shlink\Installer\Config\Plugin\DatabaseConfigCustomizer;
9
use Shlinkio\Shlink\Installer\Config\Plugin\LanguageConfigCustomizer;
10
use Shlinkio\Shlink\Installer\Config\Plugin\UrlShortenerConfigCustomizer;
11
use Zend\Stdlib\ArraySerializableInterface;
12
13
final class CustomizableAppConfig implements ArraySerializableInterface
14
{
15
    public const SQLITE_DB_PATH = 'data/database.sqlite';
16
17
    /**
18
     * @var array
19
     */
20
    private $database = [];
21
    /**
22
     * @var array
23
     */
24
    private $urlShortener = [];
25
    /**
26
     * @var array
27
     */
28
    private $language = [];
29
    /**
30
     * @var array
31
     */
32
    private $app = [];
33
    /**
34
     * @var string|null
35
     */
36
    private $importedInstallationPath;
37
38 4
    public function getDatabase(): array
39
    {
40 4
        return $this->database;
41
    }
42
43 6
    public function setDatabase(array $database): self
44
    {
45 6
        $this->database = $database;
46 6
        return $this;
47
    }
48
49 5
    public function hasDatabase(): bool
50
    {
51 5
        return ! empty($this->database);
52
    }
53
54 3
    public function getUrlShortener(): array
55
    {
56 3
        return $this->urlShortener;
57
    }
58
59 5
    public function setUrlShortener(array $urlShortener): self
60
    {
61 5
        $this->urlShortener = $urlShortener;
62 5
        return $this;
63
    }
64
65 4
    public function hasUrlShortener(): bool
66
    {
67 4
        return ! empty($this->urlShortener);
68
    }
69
70 4
    public function getLanguage(): array
71
    {
72 4
        return $this->language;
73
    }
74
75 5
    public function setLanguage(array $language): self
76
    {
77 5
        $this->language = $language;
78 5
        return $this;
79
    }
80
81 4
    public function hasLanguage(): bool
82
    {
83 4
        return ! empty($this->language);
84
    }
85
86 5
    public function getApp(): array
87
    {
88 5
        return $this->app;
89
    }
90
91 6
    public function setApp(array $app): self
92
    {
93 6
        $this->app = $app;
94 6
        return $this;
95
    }
96
97 5
    public function hasApp(): bool
98
    {
99 5
        return ! empty($this->app);
100
    }
101
102 2
    public function getImportedInstallationPath(): ?string
103
    {
104 2
        return $this->importedInstallationPath;
105
    }
106
107 1
    public function setImportedInstallationPath(string $importedInstallationPath): self
108
    {
109 1
        $this->importedInstallationPath = $importedInstallationPath;
110 1
        return $this;
111
    }
112
113
    public function hasImportedInstallationPath(): bool
114
    {
115
        return $this->importedInstallationPath !== null;
116
    }
117
118 2
    public function exchangeArray(array $array): void
119
    {
120 2
        $pathCollection = new PathCollection($array);
121
122 2
        $this->setApp($this->mapExistingPathsToKeys([
123 2
            ApplicationConfigCustomizer::SECRET => ['app_options', 'secret_key'],
124
            ApplicationConfigCustomizer::DISABLE_TRACK_PARAM => ['app_options', 'disable_track_param'],
125
            ApplicationConfigCustomizer::CHECK_VISITS_THRESHOLD => ['delete_short_urls', 'check_visits_threshold'],
126
            ApplicationConfigCustomizer::VISITS_THRESHOLD => ['delete_short_urls', 'visits_threshold'],
127 2
        ], $pathCollection));
128
129 2
        $this->setDatabase($this->mapExistingPathsToKeys([
130 2
            DatabaseConfigCustomizer::DRIVER => ['entity_manager', 'connection', 'driver'],
131
            DatabaseConfigCustomizer::USER => ['entity_manager', 'connection', 'user'],
132
            DatabaseConfigCustomizer::PASSWORD => ['entity_manager', 'connection', 'password'],
133
            DatabaseConfigCustomizer::NAME => ['entity_manager', 'connection', 'dbname'],
134
            DatabaseConfigCustomizer::HOST => ['entity_manager', 'connection', 'host'],
135
            DatabaseConfigCustomizer::PORT => ['entity_manager', 'connection', 'port'],
136 2
        ], $pathCollection));
137
138 2
        $this->setLanguage($this->mapExistingPathsToKeys([
139 2
            LanguageConfigCustomizer::DEFAULT_LANG => ['translator', 'locale'],
140
            LanguageConfigCustomizer::CLI_LANG => ['cli', '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 2
        ], $pathCollection));
149 2
    }
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 2
                $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
                'disable_track_param' => $this->app[ApplicationConfigCustomizer::DISABLE_TRACK_PARAM] ?? null,
170
            ],
171
            'delete_short_urls' => [
172
                '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
            'cli' => [
184 3
                'locale' => $this->language[LanguageConfigCustomizer::CLI_LANG] ?? 'en',
185
            ],
186
            'url_shortener' => [
187
                'domain' => [
188 3
                    'schema' => $this->urlShortener[UrlShortenerConfigCustomizer::SCHEMA] ?? 'http',
189 3
                    'hostname' => $this->urlShortener[UrlShortenerConfigCustomizer::HOSTNAME] ?? '',
190
                ],
191 3
                'shortcode_chars' => $this->urlShortener[UrlShortenerConfigCustomizer::CHARS] ?? '',
192
                'validate_url' => $this->urlShortener[UrlShortenerConfigCustomizer::VALIDATE_URL] ?? true,
193
            ],
194
        ];
195
196
        // Build dynamic database config based on selected driver
197 3
        if ($dbDriver === 'pdo_sqlite') {
198
            $config['entity_manager']['connection']['path'] = self::SQLITE_DB_PATH;
199
        } else {
200 3
            $config['entity_manager']['connection']['user'] = $this->database[DatabaseConfigCustomizer::USER] ?? '';
201 3
            $config['entity_manager']['connection']['password'] =
202 3
                $this->database[DatabaseConfigCustomizer::PASSWORD] ?? '';
203 3
            $config['entity_manager']['connection']['dbname'] = $this->database[DatabaseConfigCustomizer::NAME] ?? '';
204 3
            $config['entity_manager']['connection']['host'] = $this->database[DatabaseConfigCustomizer::HOST] ?? '';
205 3
            $config['entity_manager']['connection']['port'] = $this->database[DatabaseConfigCustomizer::PORT] ?? '';
206
207 3
            if ($dbDriver === 'pdo_mysql') {
208
                $config['entity_manager']['connection']['driverOptions'] = [
209
                    \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
210
                ];
211
            }
212
        }
213
214 3
        return $config;
215
    }
216
}
217