EndPoint::getAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\EndPoint;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
use PTS\PSR15Routing\Route;
10
11
class EndPoint implements RequestHandlerInterface
12
{
13
    /** @var string|null */
14
    protected $controller;
15
    /** @var string|null */
16
    protected $action;
17
18 1
    public function __construct(array $params = [])
19
    {
20 1
        foreach ($params as $name => $param) {
21 1
            if (property_exists($this, $name)) {
22 1
                $this->setProperty($name, $param);
23
            }
24
        }
25 1
    }
26
27
    /**
28
     * @param ServerRequestInterface $request
29
     *
30
     * @return ResponseInterface
31
     * @throws \BadMethodCallException
32
     */
33 1
    public function handle(ServerRequestInterface $request): ResponseInterface
34
    {
35 1
        $route = $this->getRoute($request);
36 1
        $endPoint = $this->getPoint($request);
37
38 1
        $params = $this->getParams($route);
39 1
        return $endPoint(...$params);
40
    }
41
42 1
    protected function setProperty(string $name, $value): void
43
    {
44 1
        $this->{$name} = $value;
45 1
    }
46
47
    /**
48
     * Get params from router and remove all protected params (_controller/_action/_other)
49
     *
50
     * @param Route $route
51
     *
52
     * @return array
53
     */
54 4
    protected function getParams(Route $route): array
55
    {
56
        return array_filter($route->getMatchesParams(), function ($name) {
57 3
            return $name[0] !== '_';
58 4
        }, ARRAY_FILTER_USE_KEY);
59
    }
60
61
    /**
62
     * @param ServerRequestInterface $request
63
     *
64
     * @return callable
65
     * @throws \BadMethodCallException
66
     */
67 1
    protected function getPoint(ServerRequestInterface $request): callable
68
    {
69 1
        $controller = $this->getControllerClass($request);
70 1
        $this->checkController($controller);
71
72 1
        $controller = new $controller($request);
73
74 1
        $action = $this->getAction($request) ?? 'index';
75 1
        $this->checkAction($controller, $action);
76
77 1
        return [$controller, $action];
78
    }
79
80 2
    protected function getControllerClass(ServerRequestInterface $request): string
81
    {
82 2
        return $this->controller ?? '';
83
    }
84
85
    /**
86
     * @param string $controller
87
     *
88
     * @throws \BadMethodCallException
89
     */
90 2
    protected function checkController(string $controller): void
91
    {
92 2
        if (!class_exists($controller)) {
93 1
            throw new \BadMethodCallException('Controller not found');
94
        }
95 1
    }
96
97
    /**
98
     * @param \object $controller
99
     * @param string $action
100
     *
101
     * @throws \BadMethodCallException
102
     */
103 4
    protected function checkAction($controller, string $action): void
104
    {
105 4
        if (!method_exists($controller, $action)) {
106 2
            throw new \BadMethodCallException('Action not found');
107
        }
108 2
    }
109
110 4
    protected function getAction(ServerRequestInterface $request): ?string
111
    {
112 4
        $route = $this->getRoute($request);
113 4
        $matches = $route->getMatchesParams();
114 4
        return $matches['_action'] ?? $this->action ?? null;
115
    }
116
117 1
    protected function getRoute(ServerRequestInterface $request): Route
118
    {
119 1
        return $request->getAttribute('route');
120
    }
121
}
122