Test Failed
Pull Request — master (#19)
by Flo
03:15
created

Initializer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 89
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setClass() 0 4 1
A getClass() 0 4 1
A setAction() 0 4 1
A getAction() 0 4 1
A setParams() 0 4 1
A getParams() 0 4 1
A execute() 0 15 2
A getServiceLocator() 0 4 1
1
<?php
2
/**
3
 * Class Initializer | Initializer.php
4
 *
5
 * @package Faulancer\Controller
6
 * @author  Florian Knapp <[email protected]>
7
 */
8
namespace Faulancer\Controller;
9
10
use Faulancer\Exception\MethodNotFoundException;
11
use Faulancer\Http\Response;
12
use Faulancer\Service\RequestService;
13
use Faulancer\ServiceLocator\ServiceLocator;
14
15
/**
16
 * Class Initializer
17
 */
18
class Initializer
19
{
20
21
    /** @var string */
22
    protected $class;
23
24
    /** @var string */
25
    protected $action;
26
27
    /** @var array */
28
    protected $params;
29
30
    /**
31
     * @param string $class
32
     */
33
    public function setClass(string $class)
34
    {
35
        $this->class = $class;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getClass()
42
    {
43
        return $this->class;
44
    }
45
46
    /**
47
     * @param string $action
48
     */
49
    public function setAction(string $action)
50
    {
51
        $this->action = $action;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getAction()
58
    {
59
        return $this->action;
60
    }
61
62
    /**
63
     * @param array $params
64
     */
65
    public function setParams(array $params)
66
    {
67
        $this->params = $params;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getParams()
74
    {
75
        return $this->params;
76
    }
77
78
    /**
79
     * @return Response
80
     * @throws MethodNotFoundException
81
     */
82
    public function execute()
83
    {
84
        $request    = $this->getServiceLocator()->get(RequestService::class);
85
        $className  = $this->getClass();
86
        $actionName = $this->getAction();
87
        $class      = new $className($request);
88
89
        if (!method_exists($class, $actionName)) {
90
            throw new MethodNotFoundException('Class "' . get_class($class) . '" doesn\'t have the method ' . $actionName);
91
        }
92
93
        $payload = array_map('strip_tags', $this->getParams());
94
95
        return call_user_func_array([$class, $actionName], $payload);
96
    }
97
98
    /**
99
     * @return ServiceLocator
100
     */
101
    private function getServiceLocator()
102
    {
103
        return ServiceLocator::instance();
104
    }
105
106
}