HtmlMinifierMiddlewareFactory::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 26
rs 9.7333
cc 4
nc 4
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Ctw\Middleware\HtmlMinifierMiddleware;
5
6
use Ctw\Middleware\HtmlMinifierMiddleware\Adapter\AdapterInterface;
7
use Exception;
8
use Psr\Container\ContainerInterface;
9
10
class HtmlMinifierMiddlewareFactory
11
{
12
    public function __invoke(ContainerInterface $container): HtmlMinifierMiddleware
13
    {
14
        $config = [];
15
        if ($container->has('config')) {
16
            $config = $container->get('config');
17
            assert(is_array($config));
18
            $config = $config[HtmlMinifierMiddleware::class] ?? [];
19
        }
20
21
        if (1 !== (is_countable($config) ? count($config) : 0)) {
22
            $format  = 'The config key for "%s" must be an array with one element';
23
            $message = sprintf($format, HtmlMinifierMiddleware::class);
24
            throw new Exception($message);
25
        }
26
27
        $className = array_key_first($config);
28
        assert(is_string($className));
29
30
        $adapter = $container->get($className);
31
        assert($adapter instanceof AdapterInterface);
32
33
        $middleware = new HtmlMinifierMiddleware();
34
35
        $middleware->setAdapter($adapter);
36
37
        return $middleware;
38
    }
39
}
40