Passed
Push — feature/code_improvement ( 07c142...134f8a )
by Thierry
20:35 queued 17:14
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
     *
58
     * @param string        $sClassName     The class
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...
59
     * @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...
60
     *
61
     * @return void
62
     */
63
    public function addClass($sClassName, $aOptions)
64
    {
65
        $this->aClasses[$sClassName] = $aOptions;
66
    }
67
68
    /**
69
     *
70
     * @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...
71
     * @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...
72
     *
73
     * @return void
74
     */
75
    public function addNamespace($sNamespace, $aOptions)
76
    {
77
        $this->aNamespaces[$sNamespace] = $aOptions;
78
    }
79
80
    /**
81
     * Find the options associated with a registered class name
82
     *
83
     * @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...
84
     *
85
     * @return array|null
86
     */
87
    public function getClassOptions($sClassName)
88
    {
89
        if(!key_exists($sClassName, $this->aClasses))
90
        {
91
            // Class not found
92
            return null;
93
        }
94
        return $this->aClasses[$sClassName];
95
    }
96
97
    /**
98
     * Find a callable object by class name
99
     *
100
     * @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...
101
     * @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...
102
     *
103
     * @return CallableObject|null
104
     */
105
    public function getCallableObject($sClassName, array $aOptions)
106
    {
107
        // Make sure the registered class exists
108
        if(key_exists('include', $aOptions))
109
        {
110
            require_once($aOptions['include']);
111
        }
112
        if(!class_exists($sClassName))
113
        {
114
            return null;
115
        }
116
117
        // Create the callable object
118
        $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...
119
        $this->aCallableOptions[$sClassName] = [];
120
        foreach($aOptions as $sName => $xValue)
121
        {
122
            if(in_array($sName, ['separator', 'namespace', 'protected']))
123
            {
124
                $xCallableObject->configure($sName, $xValue);
125
            }
126
            elseif(is_array($xValue) && $sName != 'include')
127
            {
128
                // These options are to be included in javascript code.
129
                $this->aCallableOptions[$sClassName][$sName] = $xValue;
130
            }
131
        }
132
        $this->aCallableObjects[$sClassName] = $xCallableObject;
133
134
        // Register the request factory for this callable object
135
        jaxon()->di()->setCallableClassRequestFactory($sClassName, $xCallableObject);
136
137
        return $xCallableObject;
138
    }
139
140
    /**
141
     * Get all registered classes
142
     *
143
     * @return array
144
     */
145
    public function getClasses()
146
    {
147
        return $this->aClasses;
148
    }
149
150
    /**
151
     * Get all registered namespaces
152
     *
153
     * @return array
154
     */
155
    public function getNamespaces()
156
    {
157
        return $this->aNamespaces;
158
    }
159
160
    /**
161
     * Get all registered callable objects
162
     *
163
     * @return array
164
     */
165
    public function getCallableObjects()
166
    {
167
        return $this->aCallableObjects;
168
    }
169
170
    /**
171
     * Get all registered callable objects options
172
     *
173
     * @return array
174
     */
175
    public function getCallableOptions()
176
    {
177
        return $this->aCallableOptions;
178
    }
179
}
180