Completed
Pull Request — master (#224)
by Alejandro
03:38
created

CustomizableAppConfig::setUrlShortener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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