Completed
Push — develop ( 2c1e6b...179509 )
by Peter
01:59
created

HelperPluginManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Draw;
12
13
use WebinoDraw\Cache\DrawCache;
14
use WebinoDraw\Draw\Helper\Element;
15
use WebinoDraw\Exception;
16
use WebinoDraw\Draw\Helper;
17
use WebinoDraw\Factory\HelperFactory;
18
use WebinoDraw\Draw\Helper\HelperInterface;
19
use WebinoDraw\Manipulator\Manipulator;
20
use WebinoDraw\VarTranslator\VarTranslator;
21
use Zend\ServiceManager\AbstractPluginManager;
22
use Zend\ServiceManager\ConfigInterface;
23
24
/**
25
 * Class HelperPluginManager
26
 */
27
class HelperPluginManager extends AbstractPluginManager
28
{
29
    /**
30
     * Default set of helpers factories
31
     *
32
     * @var array
33
     */
34
    protected $factories = [
35
        Helper\Absolutize::SERVICE => HelperFactory\AbsolutizeFactory::class,
36
        Helper\Form::SERVICE       => HelperFactory\FormFactory::class,
37
        Helper\Translate::SERVICE  => HelperFactory\TranslateFactory::class,
38
        Helper\Pagination::SERVICE => HelperFactory\PaginationFactory::class,
39
    ];
40
41
    /**
42
     * Default set of helpers
43
     *
44
     * @var array
45
     */
46
    protected $invokableClasses = [
47
        Element::SERVICE => Element::class,
48
    ];
49
50
    /**
51
     * Constructor
52
     *
53
     * After invoking parent constructor, add an initializer to inject the
54
     * attached helper, if any, to the currently requested helper.
55
     *
56
     * @param null|ConfigInterface $configuration
57
     */
58
    public function __construct(ConfigInterface $configuration = null)
59
    {
60
        parent::__construct($configuration);
61
62
        $this
63
            ->addInitializer([$this, 'injectEventManager'])
64
            ->addInitializer([$this, 'injectManipulator'])
65
            ->addInitializer([$this, 'injectVarTranslator'])
66
            ->addInitializer([$this, 'injectCache']);
67
    }
68
69
    /**
70
     * Inject a helper instance with the EventManager
71
     *
72
     * @param HelperInterface $helper
73
     */
74
    public function injectEventManager(HelperInterface $helper)
75
    {
76
        $locator = $this->getServiceLocator();
77
        if (!$locator) {
78
            return;
79
        }
80
81
        if ($locator->has('WebinoDraw')) {
82
            $helper->setEventManager($locator->get('WebinoDraw')->getEventManager());
0 ignored issues
show
Bug introduced by
The method setEventManager() does not seem to exist on object<WebinoDraw\Draw\Helper\HelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
            return;
84
        }
85
    }
86
87
    /**
88
     * Inject a helper instance with the WebinoDraw Manipulator
89
     *
90
     * @param HelperInterface $helper
91
     */
92
    public function injectManipulator(HelperInterface $helper)
93
    {
94
        $locator = $this->getServiceLocator();
95
        if (!$locator) {
96
            return;
97
        }
98
99
        if ($locator->has(Manipulator::class)) {
100
            $helper->setManipulator($locator->get(Manipulator::class));
101
            return;
102
        }
103
    }
104
105
    /**
106
     * Inject a helper instance with the WebinoDraw VarTranslator
107
     *
108
     * @param HelperInterface $helper
109
     */
110
    public function injectVarTranslator(HelperInterface $helper)
111
    {
112
        $locator = $this->getServiceLocator();
113
        if (!$locator) {
114
            return;
115
        }
116
117
        if ($locator->has(VarTranslator::class)) {
118
            $helper->setVarTranslator($locator->get(VarTranslator::class));
119
            return;
120
        }
121
    }
122
123
    /**
124
     * Inject a helper instance with the WebinoDraw DrawCache
125
     *
126
     * @param HelperInterface $helper
127
     */
128
    public function injectCache(HelperInterface $helper)
129
    {
130
        $locator = $this->getServiceLocator();
131
        if (!$locator) {
132
            return;
133
        }
134
135
        if ($locator->has(DrawCache::class)) {
136
            $helper->setCache($locator->get(DrawCache::class));
137
            return;
138
        }
139
    }
140
141
    /**
142
     * Validate the plugin
143
     *
144
     * Checks that the helper loaded is an instance of HelperInterface.
145
     *
146
     * @param mixed $plugin
147
     * @throws Exception\InvalidHelperException if invalid
148
     */
149
    public function validatePlugin($plugin)
150
    {
151
        if ($plugin instanceof HelperInterface) {
152
            // we're okay
153
            return;
154
        }
155
156
        throw new Exception\InvalidHelperException(
157
            sprintf(
158
                'Plugin of type %s is invalid; must implement %s\Helper\HelperInterface',
159
                (is_object($plugin) ? get_class($plugin) : gettype($plugin)),
160
                __NAMESPACE__
161
            )
162
        );
163
    }
164
}
165