Completed
Push — master ( b5e077...492522 )
by WEBEWEB
02:19
created

AbstractThemeManager::getProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Manager;
13
14
use Twig_Environment;
15
use WBW\Bundle\BootstrapBundle\Provider\ThemeProviderInterface;
16
use WBW\Library\Core\Argument\ObjectHelper;
17
18
/**
19
 * Abstract theme manager.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Bundle\BootstrapBundle\Manager
23
 * @abstract
24
 */
25
abstract class AbstractThemeManager extends AbstractManager {
26
27
    /**
28
     * Index.
29
     *
30
     * @var array
31
     */
32
    private $index;
33
34
    /**
35
     * Twig service.
36
     *
37
     * @var Twig_Environment
38
     */
39
    private $twig;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param Twig_Environment $twig The Twig service.
45
     */
46
    public function __construct(Twig_Environment $twig) {
47
        parent::__construct();
48
        $this->setIndex($this->initIndex());
49
        $this->setTwig($twig);
50
    }
51
52
    /**
53
     * Add the global.
54
     *
55
     * @return void
56
     */
57
    public function addGlobal() {
58
        foreach ($this->getIndex() as $k => $v) {
59
            if (null === $v) {
60
                continue;
61
            }
62
            $this->getTwig()->addGlobal(str_replace("Interface", "", $k), $this->getProviders()[$v]);
63
        }
64
    }
65
66
    /**
67
     * Get the index.
68
     *
69
     * @return array Returns the index.
70
     */
71
    protected function getIndex() {
72
        return $this->index;
73
    }
74
75
    /**
76
     * Get a provider.
77
     *
78
     * @param string $provider The provider class name.
79
     * @return ThemeProviderInterface Returns the theme provider in case of success, null otherwise.
80
     */
81
    protected function getProvider($provider) {
82
        $k = ObjectHelper::getShortName($provider);
83
        $v = $this->getIndex()[$k];
84
        if (null === $v) {
85
            return null;
86
        }
87
        return $this->getProviders()[$v];
88
    }
89
90
    /**
91
     * Get the Twig.
92
     *
93
     * @return Twig_Environment Returns the Twig.
94
     */
95
    public function getTwig() {
96
        return $this->twig;
97
    }
98
99
    /**
100
     * Initialize the index.
101
     *
102
     * @return array Returns the initialized index.
103
     */
104
    abstract protected function initIndex();
105
106
    /**
107
     * Set the index.
108
     *
109
     * @param array $index The index.
110
     * @return ManagerInterface Returns this manager.
111
     */
112
    protected function setIndex(array $index) {
113
        $this->index = $index;
114
        return $this;
115
    }
116
117
    /**
118
     * Set a provider.
119
     *
120
     * @param mixed $provider The provider class name.
121
     * @param ThemeProviderInterface $object The theme provider.
122
     * @return ManagerInterface Returns this manager.
123
     */
124
    protected function setProvider($provider, ThemeProviderInterface $object) {
125
        $k = ObjectHelper::getShortName($provider);
126
        $v = $this->getIndex()[$k];
127
        if (null !== $v) {
128
            $this->getProviders()[$v] = $object;
129
            return;
130
        }
131
        $this->index[$k] = count($this->getProviders());
132
        return $this->registerProvider($object);
133
    }
134
135
    /**
136
     * Set the Twig.
137
     *
138
     * @param Twig_Environment $twig The Twig.
139
     * @return ManagerInterface Returns this manager.
140
     */
141
    protected function setTwig(Twig_Environment $twig) {
142
        $this->twig = $twig;
143
        return $this;
144
    }
145
146
}
147