Completed
Push — master ( d34cec...faecd8 )
by Augusto
02:22
created

ClassName::runTarget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
/*
3
 * This file is part of the Respect\Rest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Respect\Rest\Routes;
10
11
use InvalidArgumentException;
12
use ReflectionClass;
13
use ReflectionMethod;
14
use Respect\Rest\Routable;
15
16
/** A route that builds an instance of a class to run it */
17
class ClassName extends AbstractRoute
18
{
19
    /** @var string The class name this route will instantiate */
20
    public $class = '';
21
22
    /** @var array Constructor params for the built instance */
23
    public $constructorParams = array();
24
25
    /** @var object The built class instance */
26
    protected $instance = null;
27
28
    /**
29
     * @param string $method  The HTTP method (GET, POST, etc)
30
     * @param string $pattern The URI pattern for this route (like /users/*)
31
     * @param string $class   The class name
32
     * @param array  $params  Constructor params for the class
33
     *
34
     * @see Respect\Rest\Routes\ClassName::$class
35
     * @see Respect\Rest\Routes\ClassName::$constructorParams
36
     */
37 10
    public function __construct(
38
        $method,
39
        $pattern,
40
        $class,
41
        array $params = array()
42
    ) {
43 10
        $this->class = $class;
44 10
        $this->constructorParams = $params;
45 10
        parent::__construct($method, $pattern);
46 10
    }
47
48
    /**
49
     * Creates an instance of the class this route builds
50
     *
51
     * @return object The created instance
52
     */
53 6
    protected function createInstance()
54
    {
55 6
        $className = $this->class;
56 6
        $reflection = new ReflectionClass($className);
57
58 6
        if (!$reflection->implementsInterface('Respect\\Rest\\Routable')) {
59 1
            throw new InvalidArgumentException(
60
                'Routed classes must implement Respect\\Rest\\Routable'
61 1
            );
62
        }
63
64
        if (
65 5
            empty($this->constructorParams)
66 2
            || !method_exists($this->class, '__construct')
67 5
        ) {
68 3
            return new $className();
69
        }
70
71 2
        $reflection = new ReflectionClass($this->class);
72
73 2
        return $reflection->newInstanceArgs($this->constructorParams);
74
    }
75
76
    /**
77
     * Gets the reflection for a specific method. For this route, the reflection
78
     * is given for the class method having the same name as the HTTP method.
79
     *
80
     * @param string $method The HTTP method for this implementation
81
     *
82
     * @return ReflectionMethod The returned reflection object
83
     */
84 4
    public function getReflection($method)
85
    {
86 4
        $mirror = new ReflectionClass($this->class);
87
88 4
        if ($mirror->hasMethod($method)) {
89 3
            return new ReflectionMethod($this->class, $method);
90
        }
91 1
    }
92
93
    /**
94
     * Runs the class method when this route is matched with params
95
     *
96
     * @param string $method The HTTP method for this implementation
97
     * @param array  $params An array of params for this request
98
     *
99
     * @see Respect\Rest\Request::$params
100
     *
101
     * @return mixed Whatever the class method returns
102
     */
103 6
    public function runTarget($method, &$params)
104
    {
105 6
        if (is_null($this->instance)) {
106 6
            $this->instance = $this->createInstance();
107 5
        }
108
109 5
        return call_user_func_array(
110 5
            array($this->instance, $method),
111
            $params
112 5
        );
113
    }
114
}
115