Passed
Push — feature/code_improvement ( 374d0e...1b7fb9 )
by Thierry
02:42
created

CallableDir::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * CallableDir.php - Jaxon callable dir plugin
5
 *
6
 * This class registers directories containing user defined callable classes,
7
 * and generates client side javascript code.
8
 *
9
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
10
 * @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...
11
 * @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...
12
 * @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...
13
 * @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...
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @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...
16
 * @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...
17
 * @copyright 2016 Thierry Feuzeu <[email protected]>
18
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
19
 * @link https://github.com/jaxon-php/jaxon-core
20
 */
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...
21
22
namespace Jaxon\Request\Plugin;
23
24
use Jaxon\Jaxon;
25
use Jaxon\Plugin\Request as RequestPlugin;
26
use Jaxon\Request\Support\CallableRegistry;
27
28
class CallableDir extends RequestPlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableDir
Loading history...
29
{
30
    use \Jaxon\Features\Translator;
31
32
    /**
33
     * The callable registrar
34
     *
35
     * @var CallableRegistry
36
     */
37
    protected $xRegistry;
38
39
    /**
40
     * The class constructor
41
     *
42
     * @param CallableRegistry        $xRegistry
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
43
     */
44
    public function __construct(CallableRegistry $xRegistry)
45
    {
46
        $this->xRegistry = $xRegistry;
47
    }
48
49
    /**
50
     * Return the name of this plugin
51
     *
52
     * @return string
53
     */
54
    public function getName()
55
    {
56
        return Jaxon::CALLABLE_DIR;
57
    }
58
59
    /**
60
     * Check the directory
61
     *
62
     * @param string        $sDirectory     The path of teh directory being registered
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; 5 found
Loading history...
63
     *
64
     * @return string
65
     * @throws \Jaxon\Exception\Error
66
     */
67
    private function checkDirectory($sDirectory)
68
    {
69
        if(!is_string($sDirectory))
70
        {
71
            throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration'));
72
        }
73
        $sDirectory = rtrim(trim($sDirectory), '/\\');
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $sDirectory. This often makes code more readable.
Loading history...
74
        if(!is_dir($sDirectory))
75
        {
76
            throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration'));
77
        }
78
        return realpath($sDirectory);
79
    }
80
81
    /**
82
     * Check the options
83
     *
84
     * @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 1 spaces after parameter name; 7 found
Loading history...
85
     *
86
     * @return array
87
     * @throws \Jaxon\Exception\Error
88
     */
89
    private function checkOptions($aOptions)
90
    {
91
        if(is_string($aOptions))
92
        {
93
            $aOptions = ['namespace' => $aOptions];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $aOptions. This often makes code more readable.
Loading history...
94
        }
95
        if(!is_array($aOptions))
96
        {
97
            throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration'));
98
        }
99
100
        // Change the keys in $aOptions to have "\" as separator
101
        $_aOptions = [];
102
        foreach($aOptions as $sName => $aOption)
103
        {
104
            $sName = trim(str_replace('.', '\\', $sName), ' \\');
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...
105
            $_aOptions[$sName] = $aOption;
106
        }
107
        return $_aOptions;
108
    }
109
110
    /**
111
     * Register a callable class
112
     *
113
     * @param string        $sType          The type of request handler being registered
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 10 found
Loading history...
114
     * @param string        $sDirectory     The path of teh directory being registered
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...
115
     * @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...
116
     *
117
     * @return boolean
118
     */
119
    public function register($sType, $sDirectory, $aOptions)
120
    {
121
        $sType = trim($sType);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $sType. This often makes code more readable.
Loading history...
122
        if($sType != $this->getName())
123
        {
124
            return false;
125
        }
126
127
        $sDirectory = $this->checkDirectory($sDirectory);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $sDirectory. This often makes code more readable.
Loading history...
128
129
        $aOptions = $this->checkOptions($aOptions);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 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...
Coding Style introduced by
Consider using a different name than the parameter $aOptions. This often makes code more readable.
Loading history...
130
        $aOptions['directory'] = $sDirectory;
131
132
        $sNamespace = key_exists('namespace', $aOptions) ? $aOptions['namespace'] : '';
133
        if(!($sNamespace = trim($sNamespace, ' \\')))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
134
        {
135
            $sNamespace = '';
136
        }
137
138
        if(($sNamespace))
139
        {
140
            $this->xRegistry->addNamespace($sNamespace, $aOptions);
141
        }
142
        else
143
        {
144
            $this->xRegistry->addDirectory($sDirectory, $aOptions);
145
        }
146
147
        return true;
148
    }
149
150
    /**
151
     * Generate a hash for the registered callable objects
152
     *
153
     * @return string
154
     */
155
    public function generateHash()
156
    {
157
        return '';
158
    }
159
160
    /**
161
     * Generate client side javascript code for the registered callable objects
162
     *
163
     * @return string
164
     */
165
    public function getScript()
166
    {
167
        return '';
168
    }
169
170
    /**
171
     * Check if this plugin can process the incoming Jaxon request
172
     *
173
     * @return boolean
174
     */
175
    public function canProcessRequest()
176
    {
177
        return false;
178
    }
179
180
    /**
181
     * Process the incoming Jaxon request
182
     *
183
     * @return boolean
184
     */
185
    public function processRequest()
186
    {
187
        return false;
188
    }
189
}
190