Passed
Push — main ( 56d4b4...ff7d4b )
by Daniel
05:19
created

ConfigProvider::getAssetPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Config;
6
7
use Configula\ConfigValues;
8
use Psr\Log\LogLevel;
9
use Uxmp\Core\Component\Cache\CacheDescriptorEnum;
10
use Uxmp\Core\Component\Cache\CacheTypeEnum;
11
12
/**
13
 * Provides typed access to config values
14
 */
15
final class ConfigProvider implements ConfigProviderInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private const PROTO_HTTP = 'http';
21
22
    /**
23
     * @var string
24
     */
25
    private const PROTO_HTTPS = 'https';
26
27
    /**
28
     * @var int
29
     */
30
    private const HTTP_PORT = 80;
31
32
    /**
33
     * @var int
34
     */
35
    private const HTTPS_PORT = 443;
36
37
    // Default values (ten days)
38
    /**
39
     * @var int
40
     */
41
    private const DEFAULT_TOKEN_LIFETIME = 1_086_400;
0 ignored issues
show
Bug introduced by
The constant Uxmp\Core\Component\Config\1_086_400 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42
43
    /**
44
     * @param ConfigValues<string, mixed> $configValues
45
     */
46 20
    public function __construct(
47
        private readonly ConfigValues $configValues,
48
    ) {
49 20
    }
50
51
    /**
52
     * Path to the log file storage folder
53
     */
54 1
    public function getLogFilePath(): string
55
    {
56 1
        return $this->configValues->get('logging.path', '');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...get('logging.path', '') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
57
    }
58
59
    /**
60
     * JWT secret which must be explicitly set by the user in the config
61
     */
62 1
    public function getJwtSecret(): string
63
    {
64 1
        return $this->configValues->get('security.jwt_secret', '');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...curity.jwt_secret', '') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
65
    }
66
67
    /**
68
     * Name of the http cookie needed by the streaming routes
69
     */
70 1
    public function getCookieName(): string
71
    {
72 1
        return $this->configValues->get('security.token_name', 'nekot');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...y.token_name', 'nekot') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    /**
76
     * Max age of the jwt token
77
     */
78 1
    public function getTokenLifetime(): int
79
    {
80 1
        return (int) $this->configValues->get(
81 1
            'security.token_lifetime',
82 1
            self::DEFAULT_TOKEN_LIFETIME
83 1
        );
84
    }
85
86
    /**
87
     * Log level as defined by PSR
88
     *
89
     * @phpstan-return LogLevel::*
90
     */
91 1
    public function getLogLevel(): string
92
    {
93 1
        return $this->configValues->get('logging.level', LogLevel::ERROR);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...sr\Log\LogLevel::ERROR) could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
94
    }
95
96
    /**
97
     * Name of the CORS origin (e.g. if backend/frontend hostnames differ)
98
     */
99 1
    public function getCorsOrigin(): string
100
    {
101 1
        return $this->configValues->get('http.cors_origin', '');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...'http.cors_origin', '') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
102
    }
103
104
    /**
105
     * Base path to the public folder
106
     */
107 4
    public function getApiBasePath(): string
108
    {
109 4
        return $this->configValues->get('http.api_base_path', '');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...ttp.api_base_path', '') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
110
    }
111
112
    /**
113
     * Path to the asset storage folder
114
     */
115 1
    public function getAssetPath(): string
116
    {
117 1
        return $this->configValues->get('assets.path', '');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...>get('assets.path', '') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
118
    }
119
120
    /**
121
     * Debug mode setting
122
     */
123 1
    public function getDebugMode(): bool
124
    {
125 1
        return (bool) $this->configValues->get('debug.enabled', false);
126
    }
127
128
    /**
129
     * DSN for the database connection
130
     */
131 1
    public function getDatabaseDsn(): string
132
    {
133 1
        return $this->configValues->get('database.dsn', '');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...get('database.dsn', '') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
134
    }
135
136
    /**
137
     * Database password
138
     */
139 1
    public function getDatabasePassword(): string
140
    {
141 1
        return $this->configValues->get('database.password', '');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...database.password', '') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
142
    }
143
144
    /**
145
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
146
     *  orm_query: CacheTypeEnum,
147
     *  orm_result: CacheTypeEnum,
148
     *  orm_metadata: CacheTypeEnum
149
     * }
150
     */
151 2
    public function getCacheConfig(): array
152
    {
153
        /**
154
         * @var array{
155
         *  orm_query: string,
156
         *  orm_result: string,
157
         *  orm_metadata: string
158
         * } $cacheConfig
159
         */
160 2
        $cacheConfig = (array) $this->configValues->get('cache') + [
161 2
            CacheDescriptorEnum::ORM_QUERY->value => '',
162 2
            CacheDescriptorEnum::ORM_RESULT->value => '',
163 2
            CacheDescriptorEnum::ORM_METADATA->value => '',
164 2
        ];
165
166 2
        return array_map(
167 2
            static fn (string $cacheAdapter): CacheTypeEnum => CacheTypeEnum::tryFrom($cacheAdapter) ?? CacheTypeEnum::ARRAY,
168 2
            $cacheConfig
169 2
        );
170
    }
171
172
    /**
173
     * Returns the redis connection uri
174
     */
175 1
    public function getRedisUri(): string
176
    {
177 1
        return (string) $this->configValues->get('services.redis_uri', '');
178
    }
179
180
    /**
181
     * The base url for the usage in absolute urls
182
     */
183 3
    public function getBaseUrl(): string
184
    {
185 3
        $hostname = $this->configValues->get('http.hostname', '');
186 3
        $port = (int) $this->configValues->get('http.port', 0);
187 3
        $ssl = (bool) $this->configValues->get('http.ssl', true);
188
189 3
        $protocol = ($ssl)
190 2
            ? self::PROTO_HTTPS
191 1
            : self::PROTO_HTTP;
192
193 3
        $port_string = '';
194
        if (
195 3
            $port !== 0 &&
196 3
            !in_array($port, [self::HTTP_PORT, self::HTTPS_PORT], true)
197
        ) {
198 1
            $port_string = sprintf(':%d', $port);
199
        }
200
201 3
        return sprintf(
202 3
            '%s://%s%s%s',
203 3
            $protocol,
204 3
            $hostname,
0 ignored issues
show
Bug introduced by
It seems like $hostname can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

204
            /** @scrutinizer ignore-type */ $hostname,
Loading history...
205 3
            $port_string,
206 3
            $this->getApiBasePath()
207 3
        );
208
    }
209
210
    /**
211
     * Max age of files in the clients' asset cache
212
     */
213 1
    public function getClientCacheMaxAge(): int
214
    {
215 1
        return 86_400 * 100;
0 ignored issues
show
Bug introduced by
The constant Uxmp\Core\Component\Config\86_400 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
216
    }
217
218
    /**
219
     * Destination directory for the orm proxy generation
220
     */
221 1
    public function getOrmProxyPath(): string
222
    {
223 1
        return $this->configValues->get('database.proxy_path', '');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configValu...tabase.proxy_path', '') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
224
    }
225
226
    /**
227
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
228
     *  transcoder: string,
229
     *  codec: string,
230
     *  bitrate: int,
231
     *  allowed_bitrates: array<int>,
232
     *  allowed_codecs: array<string>,
233
     * }
234
     */
235 1
    public function getTranscodingConfig(): array
236
    {
237 1
        $config = $this->configValues->get('transcoding');
238
239 1
        return [
240 1
            'transcoder' => (string) $config['transcoder'],
241 1
            'codec' => (string) $config['codec'],
242 1
            'bitrate' => (int) $config['bitrate'],
243 1
            'allowed_bitrates' => array_map('intval', explode(',', (string) $config['allowed_bitrates'])),
244 1
            'allowed_codecs' => explode(',', (string) $config['allowed_codecs']),
245 1
        ];
246
    }
247
}
248