Completed
Push — master ( a1e07d...4890bb )
by Sébastien
03:40
created

JwtAuthMiddlewareFactory::__invoke()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.1967

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 10
cts 13
cp 0.7692
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 13
nc 6
nop 1
crap 4.1967
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\Wallit\Middleware;
6
7
use Psr\Container\ContainerInterface;
8
use Soluble\Wallit\Config\ConfigProvider;
9
use Soluble\Wallit\Exception\ConfigException;
10
use Soluble\Wallit\Service\JwtService;
11
12
class JwtAuthMiddlewareFactory
13
{
14
    public const CONFIG_KEY = 'token-auth-middleware';
15
16
    /**
17
     * @param ContainerInterface $container
18
     *
19
     * @return JwtAuthMiddleware
20
     */
21 2
    public function __invoke(ContainerInterface $container): JwtAuthMiddleware
22
    {
23 2
        $config = $container->has('config') ? $container->get('config') : [];
24 2
        $options = $config[ConfigProvider::CONFIG_PREFIX][self::CONFIG_KEY] ?? null;
25
26 2
        if (!is_array($options)) {
27 1
            throw new ConfigException(sprintf(
28 1
                    "Missing or invalid entry ['%s']['%s'] in container configuration.",
29 1
                    ConfigProvider::CONFIG_PREFIX,
30 1
                    self::CONFIG_KEY)
31
            );
32
        }
33
34 1
        if (!$container->has(JwtService::class)) {
35
            throw new ConfigException(sprintf(
36
                    "Cannot locate '%s' from container, was it provided ?",
37
                    JwtService::class)
38
            );
39
        }
40
41 1
        return new JwtAuthMiddleware($container->get(JwtService::class), $options);
42
    }
43
}
44