Completed
Push — master ( 84dbb5...744962 )
by Thierry
01:43
created

CallableClass::canProcessRequest()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 16

Duplication

Lines 10
Ratio 62.5 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 0
dl 10
loc 16
rs 9.1111
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * CallableClass.php - Jaxon callable class plugin
5
 *
6
 * This class registers user defined callable classes, generates client side javascript code,
7
 * and calls their methods on user request
8
 *
9
 * @package jaxon-core
10
 * @author Jared White
11
 * @author J. Max Wilson
12
 * @author Joseph Woolley
13
 * @author Steffen Konerow
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
16
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
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
 */
21
22
namespace Jaxon\Request\Plugin;
23
24
use Jaxon\Jaxon;
25
use Jaxon\Plugin\Request as RequestPlugin;
26
use Jaxon\Request\Support\CallableRepository;
27
28
class CallableClass extends RequestPlugin
29
{
30
    use \Jaxon\Utils\Traits\Manager;
31
    use \Jaxon\Utils\Traits\Validator;
32
    use \Jaxon\Utils\Traits\Translator;
33
34
    /**
35
     * The callable repository
36
     *
37
     * @var CallableRepository
38
     */
39
    protected $repository = null;
40
41
    /**
42
     * The value of the class parameter of the incoming Jaxon request
43
     *
44
     * @var string
45
     */
46
    protected $sRequestedClass = null;
47
48
    /**
49
     * The value of the method parameter of the incoming Jaxon request
50
     *
51
     * @var string
52
     */
53
    protected $sRequestedMethod = null;
54
55
    /**
56
     * The class constructor
57
     *
58
     * @param CallableRepository        $repository
59
     */
60
    public function __construct(CallableRepository $repository)
61
    {
62
        $this->repository = $repository;
63
64
        if(!empty($_GET['jxncls']))
65
        {
66
            $this->sRequestedClass = $_GET['jxncls'];
67
        }
68
        if(!empty($_GET['jxnmthd']))
69
        {
70
            $this->sRequestedMethod = $_GET['jxnmthd'];
71
        }
72
        if(!empty($_POST['jxncls']))
73
        {
74
            $this->sRequestedClass = $_POST['jxncls'];
75
        }
76
        if(!empty($_POST['jxnmthd']))
77
        {
78
            $this->sRequestedMethod = $_POST['jxnmthd'];
79
        }
80
    }
81
82
    /**
83
     * Return the name of this plugin
84
     *
85
     * @return string
86
     */
87
    public function getName()
88
    {
89
        return Jaxon::CALLABLE_CLASS;
90
    }
91
92
    /**
93
     * Register a callable class
94
     *
95
     * @param string        $sType          The type of request handler being registered
96
     * @param string        $sClassName     The name of the class being registered
97
     * @param array|string  $aOptions       The associated options
98
     *
99
     * @return boolean
100
     */
101
    public function register($sType, $sClassName, $aOptions)
102
    {
103
        if($sType != $this->getName())
104
        {
105
            return false;
106
        }
107
108
        if(!is_string($sClassName))
109
        {
110
            throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration'));
111
        }
112
        if(is_string($aOptions))
113
        {
114
            $aOptions = ['include' => $aOptions];
115
        }
116
        if(!is_array($aOptions))
117
        {
118
            throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration'));
119
        }
120
121
        $this->repository->addClass($sClassName, $aOptions);
122
123
        return true;
124
    }
125
126
    /**
127
     * Generate a hash for the registered callable objects
128
     *
129
     * @return string
130
     */
131
    public function generateHash()
132
    {
133
        return $this->repository->generateHash();
134
    }
135
136
    /**
137
     * Generate client side javascript code for the registered callable objects
138
     *
139
     * @return string
140
     */
141
    public function getScript()
142
    {
143
        return $this->repository->getScript();
144
    }
145
146
    /**
147
     * Check if this plugin can process the incoming Jaxon request
148
     *
149
     * @return boolean
150
     */
151
    public function canProcessRequest()
152
    {
153
        // Check the validity of the class name
154 View Code Duplication
        if($this->sRequestedClass !== null && !$this->validateClass($this->sRequestedClass))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
        {
156
            $this->sRequestedClass = null;
157
            $this->sRequestedMethod = null;
158
        }
159
        // Check the validity of the method name
160 View Code Duplication
        if($this->sRequestedMethod !== null && !$this->validateMethod($this->sRequestedMethod))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
        {
162
            $this->sRequestedClass = null;
163
            $this->sRequestedMethod = null;
164
        }
165
        return ($this->sRequestedClass !== null && $this->sRequestedMethod !== null);
166
    }
167
168
    /**
169
     * Process the incoming Jaxon request
170
     *
171
     * @return boolean
172
     */
173
    public function processRequest()
174
    {
175
        if(!$this->canProcessRequest())
176
        {
177
            return false;
178
        }
179
180
        $aArgs = $this->getRequestManager()->process();
181
182
        // Find the requested method
183
        $xCallableObject = $this->repository->getCallableObject($this->sRequestedClass);
184
        if(!$xCallableObject || !$xCallableObject->hasMethod($this->sRequestedMethod))
185
        {
186
            // Unable to find the requested object or method
187
            throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid',
188
                ['class' => $this->sRequestedClass, 'method' => $this->sRequestedMethod]));
189
        }
190
191
        // Call the requested method
192
        $xCallableObject->call($this->sRequestedMethod, $aArgs);
193
        return true;
194
    }
195
}
196