Completed
Push — master ( b63b49...679147 )
by Sébastien
03:19
created

JwtAuthMiddlewareFactory::__invoke()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 1
crap 4
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 3
    public function __invoke(ContainerInterface $container): JwtAuthMiddleware
22
    {
23 3
        $config = $container->has('config') ? $container->get('config') : [];
24 3
        $options = $config[ConfigProvider::CONFIG_PREFIX][self::CONFIG_KEY] ?? null;
25
26 3
        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 2
        if (!$container->has(JwtService::class)) {
35 1
            throw new ConfigException(sprintf(
36 1
                    "Cannot locate required '%s' from container, was it provided ?",
37 1
                    JwtService::class)
38
            );
39
        }
40
41 1
        return new JwtAuthMiddleware($options,
42 1
                                     $container->get(JwtService::class));
43
    }
44
}
45