shlink_in_docker.local.php$0 ➔ getDbConfig()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 26
rs 9.0444
c 1
b 0
f 0
cc 6
nc 5
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink;
6
7
use Monolog\Handler\StreamHandler;
8
use Monolog\Logger;
9
10
use function explode;
11
use function Functional\contains;
12
use function Shlinkio\Shlink\Common\env;
13
14
use const Shlinkio\Shlink\Core\DEFAULT_DELETE_SHORT_URL_THRESHOLD;
0 ignored issues
show
Bug introduced by
The constant Shlinkio\Shlink\Core\DEF...ETE_SHORT_URL_THRESHOLD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
use const Shlinkio\Shlink\Core\DEFAULT_REDIRECT_CACHE_LIFETIME;
0 ignored issues
show
Bug introduced by
The constant Shlinkio\Shlink\Core\DEF...REDIRECT_CACHE_LIFETIME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
use const Shlinkio\Shlink\Core\DEFAULT_REDIRECT_STATUS_CODE;
0 ignored issues
show
Bug introduced by
The constant Shlinkio\Shlink\Core\DEFAULT_REDIRECT_STATUS_CODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
use const Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH;
0 ignored issues
show
Bug introduced by
The constant Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
use const Shlinkio\Shlink\Core\MIN_SHORT_CODES_LENGTH;
0 ignored issues
show
Bug introduced by
The constant Shlinkio\Shlink\Core\MIN_SHORT_CODES_LENGTH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
20
$helper = new class {
21
    private const DB_DRIVERS_MAP = [
22
        'mysql' => 'pdo_mysql',
23
        'maria' => 'pdo_mysql',
24
        'postgres' => 'pdo_pgsql',
25
        'mssql' => 'pdo_sqlsrv',
26
    ];
27
    private const DB_PORTS_MAP = [
28
        'mysql' => '3306',
29
        'maria' => '3306',
30
        'postgres' => '5432',
31
        'mssql' => '1433',
32
    ];
33
34
    public function getDbConfig(): array
35
    {
36
        $driver = env('DB_DRIVER');
37
        $isMysql = contains(['maria', 'mysql'], $driver);
38
        if ($driver === null || $driver === 'sqlite') {
39
            return [
40
                'driver' => 'pdo_sqlite',
41
                'path' => 'data/database.sqlite',
42
            ];
43
        }
44
45
        $driverOptions = ! $isMysql ? [] : [
46
            // 1002 -> PDO::MYSQL_ATTR_INIT_COMMAND
47
            1002 => 'SET NAMES utf8',
48
            // 1000 -> PDO::MYSQL_ATTR_USE_BUFFERED_QUERY
49
            1000 => true,
50
        ];
51
        return [
52
            'driver' => self::DB_DRIVERS_MAP[$driver],
53
            'dbname' => env('DB_NAME', 'shlink'),
54
            'user' => env('DB_USER'),
55
            'password' => env('DB_PASSWORD'),
56
            'host' => env('DB_HOST', $driver === 'postgres' ? env('DB_UNIX_SOCKET') : null),
57
            'port' => env('DB_PORT', self::DB_PORTS_MAP[$driver]),
58
            'driverOptions' => $driverOptions,
59
            'unix_socket' => $isMysql ? env('DB_UNIX_SOCKET') : null,
60
        ];
61
    }
62
63
    public function getNotFoundRedirectsConfig(): array
64
    {
65
        return [
66
            'invalid_short_url' => env('INVALID_SHORT_URL_REDIRECT_TO'),
67
            'regular_404' => env('REGULAR_404_REDIRECT_TO'),
68
            'base_url' => env('BASE_URL_REDIRECT_TO'),
69
        ];
70
    }
71
72
    public function getVisitsWebhooks(): array
73
    {
74
        $webhooks = env('VISITS_WEBHOOKS');
75
        return $webhooks === null ? [] : explode(',', $webhooks);
76
    }
77
78
    public function getRedisConfig(): ?array
79
    {
80
        $redisServers = env('REDIS_SERVERS');
81
        return $redisServers === null ? null : ['servers' => $redisServers];
82
    }
83
84
    public function getDefaultShortCodesLength(): int
85
    {
86
        $value = (int) env('DEFAULT_SHORT_CODES_LENGTH', DEFAULT_SHORT_CODES_LENGTH);
87
        return $value < MIN_SHORT_CODES_LENGTH ? MIN_SHORT_CODES_LENGTH : $value;
88
    }
89
90
    public function getMercureConfig(): array
91
    {
92
        $publicUrl = env('MERCURE_PUBLIC_HUB_URL');
93
94
        return [
95
            'public_hub_url' => $publicUrl,
96
            'internal_hub_url' => env('MERCURE_INTERNAL_HUB_URL', $publicUrl),
97
            'jwt_secret' => env('MERCURE_JWT_SECRET'),
98
        ];
99
    }
100
};
101
102
return [
103
104
    'app_options' => [
105
        'disable_track_param' => env('DISABLE_TRACK_PARAM'),
106
    ],
107
108
    'delete_short_urls' => [
109
        'check_visits_threshold' => true,
110
        'visits_threshold' => (int) env('DELETE_SHORT_URL_THRESHOLD', DEFAULT_DELETE_SHORT_URL_THRESHOLD),
111
    ],
112
113
    'entity_manager' => [
114
        'connection' => $helper->getDbConfig(),
115
    ],
116
117
    'url_shortener' => [
118
        'domain' => [
119
            'schema' => env('SHORT_DOMAIN_SCHEMA', 'http'),
120
            'hostname' => env('SHORT_DOMAIN_HOST', ''),
121
        ],
122
        'validate_url' => (bool) env('VALIDATE_URLS', false),
123
        'anonymize_remote_addr' => (bool) env('ANONYMIZE_REMOTE_ADDR', true),
124
        'visits_webhooks' => $helper->getVisitsWebhooks(),
125
        'default_short_codes_length' => $helper->getDefaultShortCodesLength(),
126
        'redirect_status_code' => (int) env('REDIRECT_STATUS_CODE', DEFAULT_REDIRECT_STATUS_CODE),
127
        'redirect_cache_lifetime' => (int) env('REDIRECT_CACHE_LIFETIME', DEFAULT_REDIRECT_CACHE_LIFETIME),
128
    ],
129
130
    'not_found_redirects' => $helper->getNotFoundRedirectsConfig(),
131
132
    'logger' => [
133
        'Shlink' => [
134
            'handlers' => [
135
                'shlink_handler' => [
136
                    'name' => StreamHandler::class,
137
                    'params' => [
138
                        'level' => Logger::INFO,
139
                        'stream' => 'php://stdout',
140
                    ],
141
                ],
142
            ],
143
        ],
144
    ],
145
146
    'dependencies' => [
147
        'aliases' => env('REDIS_SERVERS') === null ? [] : [
148
            'lock_store' => 'redis_lock_store',
149
        ],
150
    ],
151
152
    'cache' => [
153
        'redis' => $helper->getRedisConfig(),
154
    ],
155
156
    'router' => [
157
        'base_path' => env('BASE_PATH', ''),
158
    ],
159
160
    'mezzio-swoole' => [
161
        'swoole-http-server' => [
162
            'port' => (int) env('PORT', 8080),
163
            'options' => [
164
                'worker_num' => (int) env('WEB_WORKER_NUM', 16),
165
                'task_worker_num' => (int) env('TASK_WORKER_NUM', 16),
166
            ],
167
        ],
168
    ],
169
170
    'geolite2' => [
171
        'license_key' => env('GEOLITE_LICENSE_KEY', 'G4Lm0C60yJsnkdPi'),
172
    ],
173
174
    'mercure' => $helper->getMercureConfig(),
175
176
];
177