Completed
Push — master ( 192eed...a1e07d )
by Sébastien
03:45
created

JwtAuthMiddlewareFactory::__invoke()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.3244

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
ccs 8
cts 11
cp 0.7272
cc 4
eloc 13
nc 6
nop 1
crap 4.3244
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 2
     */
21
    public function __invoke(ContainerInterface $container): JwtAuthMiddleware
22 2
    {
23 2
        $config = $container->has('config') ? $container->get('config') : [];
24
        $options = $config[ConfigProvider::CONFIG_PREFIX][self::CONFIG_KEY] ?? null;
25 2
26 1
        if (!is_array($options)) {
27 1
            throw new ConfigException(sprintf(
28 1
                    "Missing or invalid ['%s']['%s'] entry in container configuration (config)",
29
                    ConfigProvider::CONFIG_PREFIX,
30
                    self::CONFIG_KEY)
31
            );
32 1
        }
33
34
        if (!$container->has(JwtService::class)) {
35
            throw new ConfigException(sprintf(
36
                    "Cannot locate '%s' from container, was it provided?",
37
                    JwtService::class)
38
            );
39 1
        }
40
41
        return new JwtAuthMiddleware($container->get(JwtService::class), $options);
42
    }
43
}
44