Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

ControllerInvoker   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 19 2
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Http\Dispatcher;
11
12
use Slick\Mvc\ControllerInterface;
13
use Slick\Mvc\Exception\UndefinedControllerMethodException;
14
15
/**
16
 * Controller Invoker
17
 *
18
 * @package Slick\Mvc\Http\Dispatcher
19
 * @author  Filipe Silva <[email protected]>
20
 */
21
class ControllerInvoker implements ControllerInvokerInterface
22
{
23
    /**
24
     * Invokes the controller action returning view data
25
     *
26
     * @param ControllerInterface $controller
27
     * @param ControllerDispatch $dispatch
28
     *
29
     * @return array
30
     */
31
    public function invoke(
32
        ControllerInterface $controller,
33
        ControllerDispatch $dispatch
34
    )
35
    {
36
        $reflection = new \ReflectionClass($controller);
37
38
        if (!$reflection->hasMethod($dispatch->getMethod())) {
39
            throw new UndefinedControllerMethodException(
40
                "The controller '{$dispatch->getControllerClassName()}' does ".
41
                "not have a method called '{$dispatch->getMethod()}'."
42
            );
43
        }
44
45
        $method = $reflection->getMethod($dispatch->getMethod());
46
        $method->invokeArgs($controller, $dispatch->getArguments());
47
48
        return $controller->getViewData();
49
    }
50
}