Passed
Push — feature/code_improvement ( 94564c...8399ab )
by Thierry
02:36
created

CallableRepository::getClassOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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)
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
        return $aOptions;
86
    }
87
88
    /**
89
     *
90
     * @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...
91
     * @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...
92
     * @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...
93
     *
94
     * @return void
95
     */
96
    public function addClass($sClassName, array $aClassOptions, array $aDirectoryOptions = [])
97
    {
98
        $this->aClasses[$sClassName] = $this->makeClassOptions($sClassName, $aClassOptions, $aDirectoryOptions);
99
    }
100
101
    /**
102
     *
103
     * @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...
104
     * @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...
105
     *
106
     * @return void
107
     */
108
    public function addNamespace($sNamespace, $aOptions)
109
    {
110
        $this->aNamespaces[$sNamespace] = $aOptions;
111
    }
112
113
    /**
114
     * Find the options associated with a registered class name
115
     *
116
     * @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...
117
     *
118
     * @return array|null
119
     */
120
    public function getClassOptions($sClassName)
121
    {
122
        if(!key_exists($sClassName, $this->aClasses))
123
        {
124
            // Class not found
125
            return null;
126
        }
127
        return $this->aClasses[$sClassName];
128
    }
129
130
    /**
131
     * Find a callable object by class name
132
     *
133
     * @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...
134
     * @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...
135
     *
136
     * @return CallableObject|null
137
     */
138
    public function createCallableObject($sClassName, array $aOptions)
139
    {
140
        // Make sure the registered class exists
141
        if(key_exists('include', $aOptions))
142
        {
143
            require_once($aOptions['include']);
144
        }
145
        if(!class_exists($sClassName))
146
        {
147
            return null;
148
        }
149
150
        // Create the callable object
151
        $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...
152
        $this->aCallableOptions[$sClassName] = [];
153
        foreach($aOptions as $sName => $xValue)
154
        {
155
            if(in_array($sName, ['separator', 'namespace', 'protected']))
156
            {
157
                $xCallableObject->configure($sName, $xValue);
158
            }
159
            elseif(is_array($xValue) && $sName != 'include')
160
            {
161
                // These options are to be included in javascript code.
162
                $this->aCallableOptions[$sClassName][$sName] = $xValue;
163
            }
164
        }
165
        $this->aCallableObjects[$sClassName] = $xCallableObject;
166
167
        // Register the request factory for this callable object
168
        jaxon()->di()->setCallableClassRequestFactory($sClassName, $xCallableObject);
169
170
        return $xCallableObject;
171
    }
172
173
    /**
174
     * Get all registered classes
175
     *
176
     * @return array
177
     */
178
    public function getClasses()
179
    {
180
        return $this->aClasses;
181
    }
182
183
    /**
184
     * Get all registered namespaces
185
     *
186
     * @return array
187
     */
188
    public function getNamespaces()
189
    {
190
        return $this->aNamespaces;
191
    }
192
193
    /**
194
     * Get all registered callable objects
195
     *
196
     * @return array
197
     */
198
    public function getCallableObjects()
199
    {
200
        return $this->aCallableObjects;
201
    }
202
203
    /**
204
     * Get all registered callable objects options
205
     *
206
     * @return array
207
     */
208
    public function getCallableOptions()
209
    {
210
        return $this->aCallableOptions;
211
    }
212
}
213