TokenManagerFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 29
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultIssuer() 0 3 1
A __invoke() 0 22 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service\Token;
6
7
use App\Exception\ConfigException;
8
use Psr\Container\ContainerInterface;
9
10
class TokenManagerFactory
11
{
12 2
    public function __invoke(ContainerInterface $container): TokenManager
13
    {
14 2
        $config = $container->get('config')['token_manager'] ?? null;
15 2
        if ($config === null) {
16 1
            throw new ConfigException("['token_manager'] config key is missing.");
17
        }
18 1
        if (mb_strlen($config['private_key'] ?? '') < 32) {
19
            throw new ConfigException("['tokenManager']['private_key'] config key is must be at least 32 chars long.");
20
        }
21
22 1
        if (isset($config['default_expiry']) && (
23 1
            !is_numeric($config['default_expiry']) || $config['default_expiry'] < 0
24
        )) {
25
            throw new ConfigException("['tokenManager']['default_expiry'] must be numeric > 0");
26
        }
27
28 1
        $defaultExpiry = $config['default_expiry'] ?? TokenManager::DEFAULT_EXPIRY;
29
30 1
        $issuer   = $config['default_issuer'] ?? $this->getDefaultIssuer();
31 1
        $audience = $config['default_audience'] ?? $this->getDefaultIssuer();
32
33 1
        return new TokenManager($config['private_key'], $defaultExpiry, $issuer, $audience);
34
    }
35
36 1
    public function getDefaultIssuer(): string
37
    {
38 1
        return $_SERVER['SERVER_NAME'] ?? 'localhost';
39
    }
40
}
41