Completed
Push — master ( c0f250...321e24 )
by Filipe
15:10
created

ControllerInvoker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of slick/web_stack 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\WebStack\Dispatcher;
11
12
use Slick\Di\ContainerInterface;
13
use Slick\WebStack\Controller\ControllerContextInterface;
14
use Slick\WebStack\ControllerInterface;
15
16
/**
17
 * ControllerInvoker
18
 *
19
 * @package Slick\WebStack\Dispatcher
20
 */
21
class ControllerInvoker implements ControllerInvokerInterface
22
{
23
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    /**
30
     * Creates a Controller Invoker
31
     *
32
     * @param ContainerInterface $container
33
     */
34
    public function __construct(ContainerInterface $container)
35
    {
36
        $this->container = $container;
37
    }
38
39
    /**
40
     * Invokes the controller action returning view data
41
     *
42
     * @param ControllerContextInterface $context
43
     * @param ControllerDispatch $dispatch
44
     *
45
     * @return array
46
     */
47
    public function invoke(
48
        ControllerContextInterface $context,
49
        ControllerDispatch $dispatch
50
    ) {
51
        /** @var ControllerInterface $controller */
52
        $controller = $this->createController($dispatch->controllerClass());
53
        $controller->runWithContext($context);
54
55
        $method = $dispatch->handlerMethod();
56
        $method->invokeArgs($controller, $dispatch->arguments());
57
58
        return $controller->data();
59
    }
60
61
    /**
62
     * Uses the dependency container to create the controller
63
     *
64
     * @param \ReflectionClass $controllerName
65
     *
66
     * @return ControllerInterface
67
     */
68
    private function createController(\ReflectionClass $controllerName)
69
    {
70
        $controller = $this->container->make($controllerName->getName());
71
        return $controller;
72
    }
73
}
74