AbstractControllerActionProxy::populate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 3
Bugs 1 Features 3
Metric Value
c 3
b 1
f 3
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * Fwk
4
 *
5
 * Copyright (c) 2011-2012, 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-2012 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\ContextAware;
36
use Fwk\Core\ServicesAware;
37
use Fwk\Core\Preparable;
38
use Fwk\Core\Application;
39
use Fwk\Core\Context;
40
use Fwk\Core\ActionProxy;
41
use Symfony\Component\HttpFoundation\Request;
42
use Fwk\Core\Accessor;
43
44
/**
45
 * Abstract class grouping functions/methods to manipulate controller classes.
46
 *
47
 * @category ActionProxy
48
 * @package  Fwk\Core
49
 * @author   Julien Ballestracci <[email protected]>
50
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD License
51
 * @link     http://www.fwk.pw
52
 */
53
abstract class AbstractControllerActionProxy implements ActionProxy
54
{
55
    protected $actionData = array();
56
    
57
    /**
58
     * Instantiates the controller class (must be overriden)
59
     * 
60
     * @param Application $app The running Application
61
     * 
62
     * @abstract
63
     * @return mixed
64
     */
65
    abstract protected function instantiate(Application $app);
66
    
67
    /**
68
     * Populates action class according to request params
69
     *
70
     * @param mixed   $class   Action's class
71
     * @param Request $request Current request
72
     *
73
     * @return void
74
     */
75 6
    protected function populate($class, Request $request)
76
    {
77 6
        $accessor = new Accessor($class);
78 6
        $props    = $accessor->getAttributes();
79 6
        foreach ($props as $key) {
80 4
            $value = $request->get($key, false);
81 4
            if (false !== $value) {
82 2
                $accessor->set($key, $value);
83 2
            }
84 6
        }
85 6
    }
86
    
87
    /**
88
     * Applies rules for ServicesAware, ContextAware and Preparable interfaces.
89
     * 
90
     * @param mixed       $instance Controller instance
91
     * @param Application $app      The running Application
92
     * @param Context     $context  Actual context
93
     * 
94
     * @return void
95
     */
96 6
    protected function populateCoreInterfaces($instance, Application $app, 
97
        Context $context
98
    ) {
99 6
        if ($instance instanceof ContextAware) {
100 4
            $instance->setContext($context);
101 4
        }
102
        
103 6
        if ($instance instanceof ServicesAware) {
104 4
            $instance->setServices($app->getServices());
105 4
        }
106
        
107 6
        if ($instance instanceof Preparable) {
108 4
            call_user_func(array($instance, Preparable::PREPARE_METHOD));
109 4
        }
110 6
    }
111
    
112
    /**
113
     * Executes the controller's defined method
114
     * 
115
     * @param Application $app     The running Application
116
     * @param Context     $context Actual context
117
     * 
118
     * @return mixed The controller's result
119
     */
120 6
    public function execute(Application $app, Context $context)
121
    {
122 6
        $instance = $this->instantiate($app);
123
        
124 6
        $this->populate($instance, $context->getRequest());
125 6
        $this->populateCoreInterfaces($instance, $app, $context);
126
        
127 6
        $return = call_user_func(array($instance, $this->method));
0 ignored issues
show
Bug introduced by
The property method does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
128
        
129 6
        $accessor = new Accessor($instance);
130 6
        $data = $accessor->toArray();
131
        
132
        // unset services and context from action data
133
        // to avoid having them in the view
134 6
        if ($instance instanceof ServicesAware) {
135 4
            unset($data['services']);
136 4
        } 
137
        
138 6
        if ($instance instanceof ContextAware) {
139 4
            unset($data['context']);
140 4
        } 
141
        
142 6
        $this->actionData = array_merge(
143 6
            $data,
144 6
            $this->actionData
145 6
        );
146
        
147 6
        return $return;
148
    }
149
    
150 1
    public function getActionData()
151
    {
152 1
        return $this->actionData;
153
    }
154
    
155 1
    public function setActionData(array $data)
156
    {
157 1
        $this->actionData = $data;
158
    }
159
}