Completed
Push — develop ( 8162da...f53fa5 )
by Alejandro
31s queued 13s
created

ngth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 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_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...
15
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...
16
17
$helper = new class {
18
    private const DB_DRIVERS_MAP = [
19
        'mysql' => 'pdo_mysql',
20
        'maria' => 'pdo_mysql',
21
        'postgres' => 'pdo_pgsql',
22
        'mssql' => 'pdo_sqlsrv',
23
    ];
24
    private const DB_PORTS_MAP = [
25
        'mysql' => '3306',
26
        'maria' => '3306',
27
        'postgres' => '5432',
28
        'mssql' => '1433',
29
    ];
30
31
    public function getDbConfig(): array
32
    {
33
        $driver = env('DB_DRIVER');
34
        if ($driver === null || $driver === 'sqlite') {
35
            return [
36
                'driver' => 'pdo_sqlite',
37
                'path' => 'data/database.sqlite',
38
            ];
39
        }
40
41
        $driverOptions = ! contains(['maria', 'mysql'], $driver) ? [] : [
42
            // 1002 -> PDO::MYSQL_ATTR_INIT_COMMAND
43
            1002 => 'SET NAMES utf8',
44
        ];
45
        return [
46
            'driver' => self::DB_DRIVERS_MAP[$driver],
47
            'dbname' => env('DB_NAME', 'shlink'),
48
            'user' => env('DB_USER'),
49
            'password' => env('DB_PASSWORD'),
50
            'host' => env('DB_HOST'),
51
            'port' => env('DB_PORT', self::DB_PORTS_MAP[$driver]),
52
            'driverOptions' => $driverOptions,
53
        ];
54
    }
55
56
    public function getNotFoundRedirectsConfig(): array
57
    {
58
        return [
59
            'invalid_short_url' => env('INVALID_SHORT_URL_REDIRECT_TO'),
60
            'regular_404' => env('REGULAR_404_REDIRECT_TO'),
61
            'base_url' => env('BASE_URL_REDIRECT_TO'),
62
        ];
63
    }
64
65
    public function getVisitsWebhooks(): array
66
    {
67
        $webhooks = env('VISITS_WEBHOOKS');
68
        return $webhooks === null ? [] : explode(',', $webhooks);
69
    }
70
71
    public function getRedisConfig(): ?array
72
    {
73
        $redisServers = env('REDIS_SERVERS');
74
        return $redisServers === null ? null : ['servers' => $redisServers];
75
    }
76
77
    public function getDefaultShortCodesLength(): int
78
    {
79
        $value = (int) env('DEFAULT_SHORT_CODES_LENGTH', DEFAULT_SHORT_CODES_LENGTH);
80
        return $value < MIN_SHORT_CODES_LENGTH ? MIN_SHORT_CODES_LENGTH : $value;
81
    }
82
};
83
84
return [
85
86
    'config_cache_enabled' => false,
87
88
    'app_options' => [
89
        'disable_track_param' => env('DISABLE_TRACK_PARAM'),
90
    ],
91
92
    'delete_short_urls' => [
93
        'check_visits_threshold' => true,
94
        'visits_threshold' => (int) env('DELETE_SHORT_URL_THRESHOLD', 15),
95
    ],
96
97
    'entity_manager' => [
98
        'connection' => $helper->getDbConfig(),
99
    ],
100
101
    'url_shortener' => [
102
        'domain' => [
103
            'schema' => env('SHORT_DOMAIN_SCHEMA', 'http'),
104
            'hostname' => env('SHORT_DOMAIN_HOST', ''),
105
        ],
106
        'validate_url' => (bool) env('VALIDATE_URLS', false),
107
        'visits_webhooks' => $helper->getVisitsWebhooks(),
108
        'default_short_codes_length' => $helper->getDefaultShortCodesLength(),
109
    ],
110
111
    'not_found_redirects' => $helper->getNotFoundRedirectsConfig(),
112
113
    'logger' => [
114
        'Shlink' => [
115
            'handlers' => [
116
                'shlink_handler' => [
117
                    'name' => StreamHandler::class,
118
                    'params' => [
119
                        'level' => Logger::INFO,
120
                        'stream' => 'php://stdout',
121
                    ],
122
                ],
123
            ],
124
        ],
125
    ],
126
127
    'dependencies' => [
128
        'aliases' => env('REDIS_SERVERS') === null ? [] : [
129
            'lock_store' => 'redis_lock_store',
130
        ],
131
    ],
132
133
    'cache' => [
134
        'redis' => $helper->getRedisConfig(),
135
    ],
136
137
    'router' => [
138
        'base_path' => env('BASE_PATH', ''),
139
    ],
140
141
    'mezzio-swoole' => [
142
        'swoole-http-server' => [
143
            'options' => [
144
                'worker_num' => (int) env('WEB_WORKER_NUM', 16),
145
                'task_worker_num' => (int) env('TASK_WORKER_NUM', 16),
146
            ],
147
        ],
148
    ],
149
150
];
151