Passed
Push — master ( b8ef91...1702ae )
by Thierry
02:46
created

CallableRepository::getCallableObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 2
nc 2
nop 1
rs 10
1
<?php
2
3
/**
4
 * CallableRepository.php - Jaxon callable object repository
5
 *
6
 * This class stores all the callable object already created.
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 Thierry Feuzeu <[email protected]>
10
 * @copyright 2019 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
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...
14
15
namespace Jaxon\Request\Support;
16
17
use Jaxon\Request\Factory\CallableClass\Request as RequestFactory;
18
19
use RecursiveDirectoryIterator;
20
use RecursiveIteratorIterator;
21
22
class CallableRepository
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableRepository
Loading history...
23
{
24
    /**
25
     * The classes
26
     *
27
     * These are all the registered classes.
28
     *
29
     * @var array
30
     */
31
    protected $aClasses = [];
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
32
33
    /**
34
     * The namespaces
35
     *
36
     * These are all the namespaces found in registered directories.
37
     *
38
     * @var array
39
     */
40
    protected $aNamespaces = [];
41
42
    /**
43
     * The created callable objects.
44
     *
45
     * @var array
46
     */
47
    protected $aCallableObjects = [];
48
49
    /**
50
     * The options to be applied to callable objects.
51
     *
52
     * @var array
53
     */
54
    protected $aCallableOptions = [];
55
56
    /**
57
     * Get a given class options from specified directory options
58
     *
59
     * @param string        $sClassName         The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 9 found
Loading history...
60
     * @param array         $aClassOptions      The default class options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 6 found
Loading history...
61
     * @param array         $aDirectoryOptions  The directory options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
62
     *
63
     * @return array
64
     */
65
    public function makeClassOptions($sClassName, array $aClassOptions, array $aDirectoryOptions)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
66
    {
67
        $aOptions = $aClassOptions;
68
        if(key_exists('separator', $aDirectoryOptions))
69
        {
70
            $aOptions['separator'] = $aDirectoryOptions['separator'];
71
        }
72
        if(key_exists('protected', $aDirectoryOptions))
73
        {
74
            $aOptions['protected'] = $aDirectoryOptions['protected'];
75
        }
76
        if(key_exists('*', $aDirectoryOptions))
77
        {
78
            $aOptions = array_merge($aOptions, $aDirectoryOptions['*']);
79
        }
80
        if(key_exists($sClassName, $aDirectoryOptions))
81
        {
82
            $aOptions = array_merge($aOptions, $aDirectoryOptions[$sClassName]);
83
        }
84
85
        // This value will be used to compute hash
86
        if(!key_exists('timestamp', $aOptions))
87
        {
88
            $aOptions['timestamp'] = 0;
89
        }
90
91
        return $aOptions;
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     *
96
     * @param string        $sClassName         The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 9 found
Loading history...
97
     * @param array         $aClassOptions      The default class options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 6 found
Loading history...
98
     * @param array         $aDirectoryOptions  The directory options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
99
     *
100
     * @return void
101
     */
102
    public function addClass($sClassName, array $aClassOptions, array $aDirectoryOptions = [])
103
    {
104
        $this->aClasses[$sClassName] = $this->makeClassOptions($sClassName, $aClassOptions, $aDirectoryOptions);
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
    /**
108
     *
109
     * @param string        $sNamespace     The namespace
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
110
     * @param array|string  $aOptions       The associated options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 7 found
Loading history...
111
     *
112
     * @return void
113
     */
114
    public function addNamespace($sNamespace, $aOptions)
115
    {
116
        $this->aNamespaces[$sNamespace] = $aOptions;
117
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
118
119
    /**
120
     * Find the options associated with a registered class name
121
     *
122
     * @param string        $sClassName            The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
123
     *
124
     * @return array|null
125
     */
126
    public function getClassOptions($sClassName)
127
    {
128
        if(!key_exists($sClassName, $this->aClasses))
129
        {
130
            // Class not found
131
            return null;
132
        }
133
        return $this->aClasses[$sClassName];
134
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
135
136
    /**
137
     * Find a callable object by class name
138
     *
139
     * @param string        $sClassName            The class name of the callable object
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
140
     *
141
     * @return CallableObject|null
142
     */
143
    public function getCallableObject($sClassName)
144
    {
145
        return isset($this->aCallableObjects[$sClassName]) ? $this->aCallableObjects[$sClassName] : null;
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * Create a new callable object
150
     *
151
     * @param string        $sClassName            The class name of the callable object
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
152
     * @param array         $aOptions              The callable object options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 14 found
Loading history...
153
     *
154
     * @return CallableObject|null
155
     */
156
    public function createCallableObject($sClassName, array $aOptions)
157
    {
158
        // Make sure the registered class exists
159
        if(key_exists('include', $aOptions))
160
        {
161
            require_once($aOptions['include']);
162
        }
163
        if(!class_exists($sClassName))
164
        {
165
            return null;
166
        }
167
168
        // Create the callable object
169
        $xCallableObject = new CallableObject($sClassName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 21 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...
170
        $this->aCallableOptions[$sClassName] = [];
171
        foreach($aOptions as $sName => $xValue)
172
        {
173
            if(in_array($sName, ['separator', 'namespace', 'protected']))
174
            {
175
                $xCallableObject->configure($sName, $xValue);
176
            }
177
            elseif(is_array($xValue) && $sName != 'include')
178
            {
179
                // These options are to be included in javascript code.
180
                $this->aCallableOptions[$sClassName][$sName] = $xValue;
181
            }
182
        }
183
        $this->aCallableObjects[$sClassName] = $xCallableObject;
184
185
        // Register the request factory for this callable object
186
        jaxon()->di()->setCallableClassRequestFactory($sClassName, $xCallableObject);
187
188
        return $xCallableObject;
189
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
190
191
    /**
192
     * Get all registered classes
193
     *
194
     * @return array
195
     */
196
    public function getClasses()
197
    {
198
        return $this->aClasses;
199
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
200
201
    /**
202
     * Get all registered namespaces
203
     *
204
     * @return array
205
     */
206
    public function getNamespaces()
207
    {
208
        return $this->aNamespaces;
209
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
210
211
    /**
212
     * Get all registered callable objects
213
     *
214
     * @return array
215
     */
216
    public function getCallableObjects()
217
    {
218
        return $this->aCallableObjects;
219
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
220
221
    /**
222
     * Get all registered callable objects options
223
     *
224
     * @return array
225
     */
226
    public function getCallableOptions()
227
    {
228
        return $this->aCallableOptions;
229
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
230
}
231