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

Factory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 44
ccs 20
cts 21
cp 0.9524
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getReflection() 0 11 2
A runTarget() 0 15 3
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 ReflectionMethod;
12
use InvalidArgumentException;
13
use Respect\Rest\Routable;
14
15
class Factory extends AbstractRoute
16
{
17
    public $class = '';
18
    protected $instance = null;
19
    public $factory = null;
20
21
    /** @var ReflectionMethod */
22
    protected $reflection;
23
24 3
    public function __construct($method, $pattern, $class, $factory)
25
    {
26 3
        $this->factory = $factory;
27 3
        $this->class = $class;
28 3
        parent::__construct($method, $pattern);
29 3
    }
30
31 1
    public function getReflection($method)
32
    {
33 1
        if (empty($this->reflection)) {
34 1
            $this->reflection = new ReflectionMethod(
35 1
                $this->class,
36
                $method
37 1
            );
38 1
        }
39
40 1
        return $this->reflection;
41
    }
42
43 1
    public function runTarget($method, &$params)
44
    {
45 1
        if (is_null($this->instance)) {
46 1
            $this->instance = call_user_func_array($this->factory, array($method, &$params));
47 1
        }
48
49 1
        if (!$this->instance instanceof Routable) {
50
            throw new InvalidArgumentException('Routed classes must implement the Respect\\Rest\\Routable interface');
51
        }
52
53 1
        return call_user_func_array(
54 1
            array($this->instance, $method),
55
            $params
56 1
        );
57
    }
58
}
59