Test Setup Failed
Push — master ( 9bc1fd...d8e9aa )
by Mehmet
05:08
created

ApplicationController::getControllerClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami;
5
6
use Psr\Container\ContainerInterface;
7
use Selami\Router\Router;
8
use Selami\Stdlib\Resolver;
9
use ReflectionClass;
10
11
class ApplicationController
12
{
13
    private $container;
14
    private $controller;
15
    private $controllerClass;
16
    private $returnType;
17
    private $uriParameters;
18
19
    public function __construct(
20
        ContainerInterface $container,
21
        string $controller,
22
        int $returnType = Router::HTML,
23
        ?array $uriParameters = []
24
    ) {
25
        $this->container = $container;
26
        $this->controller = $controller;
27
        $this->returnType = $returnType;
28
        $this->uriParameters = $uriParameters;
29
    }
30
31
32
    public function getUriParameters() : array
33
    {
34
        return $this->uriParameters;
35
    }
36
37
    public function getControllerResponse() : ControllerResponse
38
    {
39
        $this->controllerClass = $this->controller;
40
        $controllerConstructorArguments = Resolver::getParameterHints($this->controllerClass, '__construct');
41
        $arguments = [];
42
        foreach ($controllerConstructorArguments as $argumentName => $argumentType) {
43
            $arguments[] = $this->getArgument($argumentName, $argumentType);
44
        }
45
        $controllerClass = new ReflectionClass($this->controllerClass);
46
47
        /**
48
         * @var $controllerObject \Selami\Interfaces\Controller
49
         */
50
        $controllerObject = $controllerClass->newInstanceArgs($arguments);
51
        return $controllerObject();
52
    }
53
54
    private function getArgument(string $argumentName, string $argumentType)
55
    {
56
        if ($argumentType === Resolver::ARRAY) {
57
            return $this->container->has($argumentName) ?
58
                $this->container->get($argumentName) :  $this->{'get'.ucfirst($argumentName)}();
59
        }
60
        return $this->container->get($argumentType);
61
    }
62
}
63