Failed Conditions
Push — master ( c6060f...bdc7d6 )
by Sébastien
02:20
created

TokenManagerFactory::__invoke()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 4
nop 1
dl 0
loc 21
ccs 0
cts 17
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service;
6
7
use App\Exception\ConfigException;
8
use Psr\Container\ContainerInterface;
9
10
class TokenManagerFactory
11
{
12
    public function __invoke(ContainerInterface $container): TokenManager
13
    {
14
        $config = $container->get('config')['token_manager'] ?? null;
15
        if ($config === null) {
16
            throw new ConfigException("['token_manager'] config key is missing.");
17
        }
18
        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
        $defaultExpiry = TokenManager::DEFAULT_EXPIRY;
0 ignored issues
show
Unused Code introduced by
The assignment to $defaultExpiry is dead and can be removed.
Loading history...
23
24
		if (isset($config['default_expiry']) && (
25
				!is_numeric($config['default_expiry']) || $config['default_expiry'] < 0)) {
26
			throw new ConfigException("['tokenManager']['default_expiry'] must be numeric > 0");
27
		} else {
28
			$defaultExpiry = (int) $config['default_expiry'];
29
		}
30
31
32
        return new TokenManager($config['private_key'], $defaultExpiry);
33
    }
34
}
35