MiddlewareProcessor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B initialize() 0 27 4
1
<?php
2
/*
3
 * This file is part of the Borobudur-Kernel package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Kernel\Processor;
12
13
use Borobudur\DependencyInjection\ContainerAwareInterface;
14
use Borobudur\Kernel\Exception\InvalidArgumentException;
15
use Borobudur\Kernel\KernelInterface;
16
use Borobudur\Kernel\Middleware\MiddlewareInterface;
17
18
/**
19
 * @author      Iqbal Maulana <[email protected]>
20
 * @created     8/17/15
21
 */
22
class MiddlewareProcessor implements ProcessorInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function initialize(KernelInterface $kernel)
28
    {
29
        $middlewareContainer = $kernel->getMiddleware();
30
        $container = $kernel->getContainer();
31
        $env = $kernel->getEnvironmentManager()->getEnvInstance();
32
33
        $middlewareContainer->setContainer($container);
34
        $bundleMiddlewares = (array) $kernel->registerMiddlewares();
35
        $envMiddlewares = (array) $env->registerMiddlewares();
36
37
        foreach (array_merge($bundleMiddlewares, $envMiddlewares) as $middleware) {
38
            if ($middleware instanceof MiddlewareInterface) {
39
                if ($middleware instanceof ContainerAwareInterface) {
40
                    $middleware->setContainer($container);
41
                }
42
43
                $middlewareContainer->set($middleware);
44
                continue;
45
            }
46
47
            throw new InvalidArgumentException(sprintf(
48
                'Class "%s" should implement "%s".',
49
                get_class($middleware),
50
                'Borobudur\Kernel\Middleware\MiddlewareInterface'
51
            ));
52
        }
53
    }
54
}
55