Completed
Push — develop ( 34d8b3...fd6151 )
by Alejandro
22s queued 14s
created

shlink_in_docker.local.php$0 ➔ getVisitsWebhooks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 1
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 file_exists;
12
use function file_get_contents;
13
use function file_put_contents;
14
use function Functional\contains;
15
use function implode;
16
use function Shlinkio\Shlink\Common\env;
17
use function sprintf;
18
use function str_shuffle;
19
use function substr;
20
use function sys_get_temp_dir;
21
22
$helper = new class {
23
    private const BASE62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
24
    private const DB_DRIVERS_MAP = [
25
        'mysql' => 'pdo_mysql',
26
        'maria' => 'pdo_mysql',
27
        'postgres' => 'pdo_pgsql',
28
    ];
29
    private const DB_PORTS_MAP = [
30
        'mysql' => '3306',
31
        'maria' => '3306',
32
        'postgres' => '5432',
33
    ];
34
35
    /** @var string */
36
    private $secretKey;
37
38
    public function __construct()
39
    {
40
        [, $this->secretKey] = $this->initShlinkSecretKey();
41
    }
42
43
    private function initShlinkSecretKey(): array
44
    {
45
        $keysFile = sprintf('%s/shlink.keys', sys_get_temp_dir());
46
        if (file_exists($keysFile)) {
47
            return explode(',', file_get_contents($keysFile));
48
        }
49
50
        $keys = [
51
            '', // This was the SHORTCODE_CHARS. Kept as empty string for BC
52
            env('SECRET_KEY', $this->generateSecretKey()), // Deprecated
53
        ];
54
55
        file_put_contents($keysFile, implode(',', $keys));
56
        return $keys;
57
    }
58
59
    private function generateSecretKey(): string
60
    {
61
        return substr(str_shuffle(self::BASE62), 0, 32);
62
    }
63
64
    public function getSecretKey(): string
65
    {
66
        return $this->secretKey;
67
    }
68
69
    public function getDbConfig(): array
70
    {
71
        $driver = env('DB_DRIVER');
72
        if ($driver === null || $driver === 'sqlite') {
73
            return [
74
                'driver' => 'pdo_sqlite',
75
                'path' => 'data/database.sqlite',
76
            ];
77
        }
78
79
        $driverOptions = ! contains(['maria', 'mysql'], $driver) ? [] : [
80
            // 1002 -> PDO::MYSQL_ATTR_INIT_COMMAND
81
            1002 => 'SET NAMES utf8',
82
        ];
83
        return [
84
            'driver' => self::DB_DRIVERS_MAP[$driver],
85
            'dbname' => env('DB_NAME', 'shlink'),
86
            'user' => env('DB_USER'),
87
            'password' => env('DB_PASSWORD'),
88
            'host' => env('DB_HOST'),
89
            'port' => env('DB_PORT', self::DB_PORTS_MAP[$driver]),
90
            'driverOptions' => $driverOptions,
91
        ];
92
    }
93
94
    public function getNotFoundRedirectsConfig(): array
95
    {
96
        return [
97
            'invalid_short_url' => env('INVALID_SHORT_URL_REDIRECT_TO', env('NOT_FOUND_REDIRECT_TO')),
98
            'regular_404' => env('REGULAR_404_REDIRECT_TO'),
99
            'base_url' => env('BASE_URL_REDIRECT_TO'),
100
        ];
101
    }
102
103
    public function getVisitsWebhooks(): array
104
    {
105
        $webhooks = env('VISITS_WEBHOOKS');
106
        return $webhooks === null ? [] : explode(',', $webhooks);
107
    }
108
};
109
110
return [
111
112
    'config_cache_enabled' => false,
113
114
    'app_options' => [
115
        'secret_key' => $helper->getSecretKey(),
116
        'disable_track_param' => env('DISABLE_TRACK_PARAM'),
117
    ],
118
119
    'delete_short_urls' => [
120
        'check_visits_threshold' => true,
121
        'visits_threshold' => (int) env('DELETE_SHORT_URL_THRESHOLD', 15),
122
    ],
123
124
    'entity_manager' => [
125
        'connection' => $helper->getDbConfig(),
126
    ],
127
128
    'url_shortener' => [
129
        'domain' => [
130
            'schema' => env('SHORT_DOMAIN_SCHEMA', 'http'),
131
            'hostname' => env('SHORT_DOMAIN_HOST', ''),
132
        ],
133
        'validate_url' => (bool) env('VALIDATE_URLS', true),
134
        'visits_webhooks' => $helper->getVisitsWebhooks(),
135
    ],
136
137
    'not_found_redirects' => $helper->getNotFoundRedirectsConfig(),
138
139
    'logger' => [
140
        'Shlink' => [
141
            'handlers' => [
142
                'shlink_handler' => [
143
                    'name' => StreamHandler::class,
144
                    'params' => [
145
                        'level' => Logger::INFO,
146
                        'stream' => 'php://stdout',
147
                    ],
148
                ],
149
            ],
150
        ],
151
    ],
152
153
    'dependencies' => [
154
        'aliases' => env('REDIS_SERVERS') === null ? [] : [
155
            'lock_store' => 'redis_lock_store',
156
        ],
157
    ],
158
159
    'redis' => [
160
        'servers' => env('REDIS_SERVERS'),
161
    ],
162
163
    'router' => [
164
        'base_path' => env('BASE_PATH', ''),
165
    ],
166
167
    'zend-expressive-swoole' => [
168
        'swoole-http-server' => [
169
            'options' => [
170
                'worker_num' => (int) env('WEB_WORKER_NUM', 16),
171
                'task_worker_num' => (int) env('TASK_WORKER_NUM', 16),
172
            ],
173
        ],
174
    ],
175
176
];
177