CallableActionProxy   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Test Coverage

Coverage 83.33%

Importance

Changes 7
Bugs 1 Features 5
Metric Value
wmc 10
c 7
b 1
f 5
cbo 3
dl 0
loc 82
ccs 25
cts 30
cp 0.8333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B execute() 0 28 6
A getActionData() 0 4 1
A setActionData() 0 4 1
1
<?php
2
/**
3
 * Fwk
4
 *
5
 * Copyright (c) 2011-2014, Julien Ballestracci <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
17
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
21
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
 * POSSIBILITY OF SUCH DAMAGE.
23
 *
24
 * PHP Version 5.3
25
 *
26
 * @category  Core
27
 * @package   Fwk\Core
28
 * @author    Julien Ballestracci <[email protected]>
29
 * @copyright 2011-2014 Julien Ballestracci <[email protected]>
30
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
31
 * @link      http://www.fwk.pw
32
 */
33
namespace Fwk\Core\Action;
34
35
use Fwk\Core\ActionProxy;
36
use Fwk\Core\Application;
37
use Fwk\Core\Context;
38
39
/**
40
 * ActionProxy to a Closure or a Callable. 
41
 *
42
 * @category ActionProxy
43
 * @package  Fwk\Core
44
 * @author   Julien Ballestracci <[email protected]>
45
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD License
46
 * @link     http://www.fwk.pw
47
 */
48
class CallableActionProxy implements ActionProxy
49
{
50
    const PARAM_CONTEXT_NAME = 'context';
51
    const PARAM_SERVICES_NAME = 'services';
52
    
53
    /**
54
     * The Closure or Callable 
55
     * 
56
     * @var mixed
57
     */
58
    protected $callable;
59
    
60
    /**
61
     *
62
     * @var array
63
     */
64
    protected $actionData = array();
65
    
66
    /**
67
     * Constructor
68
     * 
69
     * @param mixed $callable Closure or Callable
70
     * 
71
     * @throws \InvalidArgumentException if invalid $callable
72
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
73
     */
74 34
    public function __construct($callable)
75
    {
76 34
        if (!is_callable($callable)) {
77 1
            throw new \InvalidArgumentException("The closure is not callable");
78
        }
79
        
80 34
        $this->callable = $callable;
81 34
    }
82
    
83
    /**
84
     * Executes the callable and returns the result
85
     * 
86
     * @param Application $app     The running Application
87
     * @param Context     $context Actual context
88
     * 
89
     * @return mixed The controller's result
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object|integer|double|string|null|boolean|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
90
     */
91 19
    public function execute(Application $app, Context $context)
92
    {
93 19
        if ($this->callable instanceof \Closure) {
94 18
            $refFunc = new \ReflectionFunction($this->callable);
95
96 18
            $params  = array();
97 18
            $request = $context->getRequest();
98 18
            foreach ($refFunc->getParameters() as $param) {
99 13
                if ($param->getName() == self::PARAM_CONTEXT_NAME) {
100 5
                    $params[] = $context;
101 13
                } elseif ($param->getName() == self::PARAM_SERVICES_NAME) {
102 3
                    $params[] = $app->getServices();
103 3
                } else {
104 6
                    $params[] = $request->get($param->getName(), null);
105
                }
106 18
            }
107
            
108 18
            $result = call_user_func_array($this->callable, $params);
109 18
        } else {
110 1
            $result = call_user_func($this->callable);
111
        }
112
        
113 19
        if (is_array($result)) {
114
            $this->actionData = array_merge($result, $this->actionData);
115
        }
116
        
117 19
        return $result;
118
    }
119
    
120 3
    public function getActionData()
121
    {
122 3
        return $this->actionData;
123
    }
124
    
125
    public function setActionData(array $data)
126
    {
127
        $this->actionData = $data;
128
    }
129
}