Completed
Pull Request — master (#472)
by Alejandro
06:28
created

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

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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