Completed
Pull Request — master (#506)
by Alejandro
13:13
created

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

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 14
rs 10
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
            // PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
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 getNotFoundConfig(): array
95
    {
96
        $notFoundRedirectTo = env('NOT_FOUND_REDIRECT_TO');
97
98
        return [
99
            'enable_redirection' => $notFoundRedirectTo !== null,
100
            'redirect_to' => $notFoundRedirectTo,
101
        ];
102
    }
103
};
104
105
return [
106
107
    'config_cache_enabled' => false,
108
109
    'app_options' => [
110
        'secret_key' => $helper->getSecretKey(),
111
        'disable_track_param' => env('DISABLE_TRACK_PARAM'),
112
    ],
113
114
    'delete_short_urls' => [
115
        'check_visits_threshold' => true,
116
        'visits_threshold' => (int) env('DELETE_SHORT_URL_THRESHOLD', 15),
117
    ],
118
119
    'entity_manager' => [
120
        'connection' => $helper->getDbConfig(),
121
    ],
122
123
    'url_shortener' => [
124
        'domain' => [
125
            'schema' => env('SHORT_DOMAIN_SCHEMA', 'http'),
126
            'hostname' => env('SHORT_DOMAIN_HOST', ''),
127
        ],
128
        'validate_url' => (bool) env('VALIDATE_URLS', true),
129
        'not_found_short_url' => $helper->getNotFoundConfig(),
130
    ],
131
132
    'logger' => [
133
        'handlers' => [
134
            'shlink_rotating_handler' => [
135
                'level' => Logger::EMERGENCY, // This basically disables regular file logs
136
            ],
137
            'shlink_stdout_handler' => [
138
                'class' => StreamHandler::class,
139
                'level' => Logger::INFO,
140
                'stream' => 'php://stdout',
141
                'formatter' => 'dashed',
142
            ],
143
        ],
144
145
        'loggers' => [
146
            'Shlink' => [
147
                'handlers' => ['shlink_stdout_handler'],
148
            ],
149
        ],
150
    ],
151
152
    'dependencies' => [
153
        'aliases' => env('REDIS_SERVERS') === null ? [] : [
154
            'lock_store' => 'redis_lock_store',
155
        ],
156
    ],
157
158
    'redis' => [
159
        'servers' => env('REDIS_SERVERS'),
160
    ],
161
162
    'router' => [
163
        'base_path' => env('BASE_PATH', ''),
164
    ],
165
166
];
167