Passed
Push — master ( ebaa0d...c158b7 )
by Thierry
03:41
created

PluginManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
eloc 48
c 3
b 0
f 0
dl 0
loc 189
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponsePlugin() 0 12 3
A registerCallable() 0 10 3
B registerPlugin() 0 36 7
A getRequestHandlers() 0 3 1
A __construct() 0 5 1
A getParameterReader() 0 3 1
A registerPlugins() 0 11 1
1
<?php
2
3
/**
4
 * PluginManager.php - Jaxon plugin registry
5
 *
6
 * Register Jaxon plugins and callables.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
10
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
11
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
12
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-core
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
20
21
namespace Jaxon\Plugin\Manager;
22
23
use Jaxon\Jaxon;
24
use Jaxon\App\I18n\Translator;
25
use Jaxon\Di\Container;
26
use Jaxon\Exception\SetupException;
27
use Jaxon\Plugin\CallableRegistryInterface;
28
use Jaxon\Plugin\Code\CodeGenerator;
29
use Jaxon\Plugin\CodeGeneratorInterface;
30
use Jaxon\Plugin\Request\CallableClass\CallableClassPlugin;
31
use Jaxon\Plugin\Request\CallableDir\CallableDirPlugin;
32
use Jaxon\Plugin\Request\CallableFunction\CallableFunctionPlugin;
33
use Jaxon\Plugin\RequestHandlerInterface;
34
use Jaxon\Plugin\RequestPlugin;
35
use Jaxon\Plugin\Response\DataBag\DataBagPlugin;
36
use Jaxon\Plugin\Response\Dialog\DialogPlugin;
37
use Jaxon\Plugin\Response\JQuery\JQueryPlugin;
38
use Jaxon\Plugin\ResponsePlugin;
39
use Jaxon\Plugin\ResponsePluginInterface;
40
use Jaxon\Request\Handler\ParameterReader;
41
use Jaxon\Response\Response;
42
43
use function class_implements;
44
use function in_array;
45
46
class PluginManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class PluginManager
Loading history...
47
{
48
    /**
49
     * @var Container
50
     */
51
    protected $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
52
53
    /**
54
     * @var CodeGenerator
55
     */
56
    private $xCodeGenerator;
57
58
    /**
59
     * @var Translator
60
     */
61
    protected $xTranslator;
62
63
    /**
64
     * Request plugins, indexed by name
65
     *
66
     * @var array<CallableRegistryInterface>
67
     */
68
    private $aRegistryPlugins = [];
69
70
    /**
71
     * Request handlers, indexed by name
72
     *
73
     * @var array<RequestHandlerInterface>
74
     */
75
    private $aRequestHandlers = [];
76
77
    /**
78
     * Response plugins, indexed by name
79
     *
80
     * @var array<ResponsePluginInterface>
81
     */
82
    private $aResponsePlugins = [];
83
84
    /**
85
     * The constructor
86
     *
87
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
88
     * @param CodeGenerator $xCodeGenerator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
89
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
90
     */
91
    public function __construct(Container $di, CodeGenerator $xCodeGenerator, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
92
    {
93
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
94
        $this->xCodeGenerator = $xCodeGenerator;
95
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
96
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
    /**
99
     * Get the request plugins
100
     *
101
     * @return array<RequestPlugin>
102
     */
103
    public function getRequestHandlers(): array
104
    {
105
        return $this->aRequestHandlers;
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Register a plugin
110
     *
111
     * Below is a table for priorities and their description:
112
     * - 0 to 999: Plugins that are part of or extensions to the jaxon core
113
     * - 1000 to 8999: User created plugins, typically, these plugins don't care about order
114
     * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list
115
     *
116
     * @param string $sClassName    The plugin class
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
117
     * @param string $sPluginName    The plugin name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
118
     * @param integer $nPriority    The plugin priority, used to order the plugins
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
119
     *
120
     * @return void
121
     * @throws SetupException
122
     */
123
    public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000)
124
    {
125
        $bIsUsed = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
126
        $aInterfaces = class_implements($sClassName);
127
        if(in_array(CodeGeneratorInterface::class, $aInterfaces))
128
        {
129
            $this->xCodeGenerator->addCodeGenerator($sClassName, $nPriority);
130
            $bIsUsed = true;
131
        }
132
        if(in_array(CallableRegistryInterface::class, $aInterfaces))
133
        {
134
            $this->aRegistryPlugins[$sPluginName] = $sClassName;
135
            $bIsUsed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 30 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
136
        }
137
        if(in_array(RequestHandlerInterface::class, $aInterfaces))
138
        {
139
            $this->aRequestHandlers[$sPluginName] = $sClassName;
140
            $bIsUsed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 30 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
141
        }
142
        if(in_array(ResponsePluginInterface::class, $aInterfaces))
143
        {
144
            $this->aResponsePlugins[$sPluginName] = $sClassName;
145
            $bIsUsed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 30 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
146
        }
147
148
        if(!$bIsUsed)
149
        {
150
            // The class is invalid.
151
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
152
            throw new SetupException($sMessage);
153
        }
154
155
        // Register the plugin in the DI container, if necessary
156
        if(!$this->di->has($sClassName))
157
        {
158
            $this->di->auto($sClassName);
159
        }
160
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
161
162
    /**
163
     * Find the specified response plugin by name and return a reference to it if one exists
164
     *
165
     * @param string $sName    The name of the plugin
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
166
     * @param Response|null $xResponse    The response to attach the plugin to
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
167
     *
168
     * @return ResponsePlugin|null
169
     */
170
    public function getResponsePlugin(string $sName, ?Response $xResponse = null): ?ResponsePlugin
171
    {
172
        if(!isset($this->aResponsePlugins[$sName]))
173
        {
174
            return null;
175
        }
176
        $xPlugin = $this->di->g($this->aResponsePlugins[$sName]);
0 ignored issues
show
Bug introduced by
$this->aResponsePlugins[$sName] of type Jaxon\Plugin\ResponsePluginInterface is incompatible with the type string expected by parameter $sClass of Jaxon\Di\Container::g(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
        $xPlugin = $this->di->g(/** @scrutinizer ignore-type */ $this->aResponsePlugins[$sName]);
Loading history...
177
        if(($xResponse))
178
        {
179
            $xPlugin->setResponse($xResponse);
180
        }
181
        return $xPlugin;
182
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
183
184
    /**
185
     * Register a function or callable class
186
     *
187
     * Call the request plugin with the $sType defined as name.
188
     *
189
     * @param string $sType    The type of request handler being registered
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
190
     * @param string $sCallable    The callable entity being registered
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
191
     * @param array|string $xOptions    The associated options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
192
     *
193
     * @return void
194
     * @throws SetupException
195
     */
196
    public function registerCallable(string $sType, string $sCallable, $xOptions = [])
197
    {
198
        if(isset($this->aRegistryPlugins[$sType]) &&
199
            ($xPlugin = $this->di->g($this->aRegistryPlugins[$sType])))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
Bug introduced by
$this->aRegistryPlugins[$sType] of type Jaxon\Plugin\CallableRegistryInterface is incompatible with the type string expected by parameter $sClass of Jaxon\Di\Container::g(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
            ($xPlugin = $this->di->g(/** @scrutinizer ignore-type */ $this->aRegistryPlugins[$sType])))
Loading history...
200
        {
201
            $xPlugin->register($sType, $sCallable, $xPlugin->checkOptions($sCallable, $xOptions));
202
            return;
203
        }
204
        throw new SetupException($this->xTranslator->trans('errors.register.plugin',
205
            ['name' => $sType, 'callable' => $sCallable]));
206
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
207
208
    /**
209
     * Register the Jaxon request plugins
210
     *
211
     * @return void
212
     * @throws SetupException
213
     */
214
    public function registerPlugins()
215
    {
216
        // Request plugins
217
        $this->registerPlugin(CallableClassPlugin::class, Jaxon::CALLABLE_CLASS, 101);
218
        $this->registerPlugin(CallableFunctionPlugin::class, Jaxon::CALLABLE_FUNCTION, 102);
219
        $this->registerPlugin(CallableDirPlugin::class, Jaxon::CALLABLE_DIR, 103);
220
221
        // Response plugins
222
        $this->registerPlugin(JQueryPlugin::class, JQueryPlugin::NAME, 700);
223
        $this->registerPlugin(DataBagPlugin::class, DataBagPlugin::NAME, 700);
224
        $this->registerPlugin(DialogPlugin::class, DialogPlugin::NAME, 750);
225
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
226
227
    /**
228
     * Get the parameter reader
229
     *
230
     * @return ParameterReader
231
     */
232
    public function getParameterReader()
233
    {
234
        return $this->di->g(ParameterReader::class);
235
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
236
}
237