Completed
Push — master ( 3a8823...81c6e7 )
by Piotr
02:46
created

ContextManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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
namespace FSi\Bundle\AdminBundle\Admin\Context;
11
12
use FSi\Bundle\AdminBundle\Admin\Element;
13
14
/**
15
 * @author Norbert Orzechowicz <[email protected]>
16
 */
17
class ContextManager
18
{
19
    /**
20
     * @var \FSi\Bundle\AdminBundle\Admin\Context\ContextInterface[]
21
     */
22
    protected $contexts;
23
24
    /**
25
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextInterface[] $contexts
26
     */
27
    public function __construct($contexts = array())
28
    {
29
        $this->contexts = array();
30
31
        foreach($contexts as $context) {
32
            $this->addContext($context);
33
        }
34
    }
35
36
    /**
37
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextInterface $builder
38
     */
39
    public function addContext(ContextInterface $builder)
40
    {
41
        $this->contexts[] = $builder;
42
    }
43
44
    /**
45
     * @param string $route
46
     * @param \FSi\Bundle\AdminBundle\Admin\Element $element
47
     * @return \FSi\Bundle\AdminBundle\Admin\Context\ContextInterface|null
48
     */
49
    public function createContext($route, Element $element)
50
    {
51
        foreach ($this->contexts as $context) {
52
            if ($context->supports($route, $element)) {
53
                $context->setElement($element);
54
                return $context;
55
            }
56
        }
57
58
        return null;
59
    }
60
}
61