ContextManager::createContext()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 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