ContextManager   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 11
dl 0
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addContext() 0 3 1
A createContext() 0 10 3
A __construct() 0 6 2
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Admin\Context;
13
14
use FSi\Bundle\AdminBundle\Admin\Element;
15
16
class ContextManager
17
{
18
    /**
19
     * @var ContextInterface[]
20
     */
21
    protected $contexts;
22
23
    /**
24
     * @param ContextInterface[] $contexts
25
     */
26
    public function __construct(array $contexts = [])
27
    {
28
        $this->contexts = [];
29
30
        foreach($contexts as $context) {
31
            $this->addContext($context);
32
        }
33
    }
34
35
    public function addContext(ContextInterface $builder): void
36
    {
37
        $this->contexts[] = $builder;
38
    }
39
40
    public function createContext(string $route, Element $element): ?ContextInterface
41
    {
42
        foreach ($this->contexts as $context) {
43
            if ($context->supports($route, $element)) {
44
                $context->setElement($element);
45
                return $context;
46
            }
47
        }
48
49
        return null;
50
    }
51
}
52