Completed
Pull Request — develop (#599)
by Alejandro
04:55 queued 01:46
created

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