HtmlMinifierMiddlewareFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 1
b 0
f 0
dl 0
loc 28
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 26 4
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