Passed
Push — master ( d5e16d...c62e96 )
by Alexpts
02:25
created

EndPoint::setProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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
	 * @return array
52
	 */
53 4
    protected function getParams(Route $route): array
54
	{
55
        return array_filter($route->getMatchesParams(), function ($name) {
56 3
            return $name[0] !== '_';
57 4
        }, ARRAY_FILTER_USE_KEY);
58
	}
59
60
    /**
61
     * @param ServerRequestInterface $request
62
     *
63
     * @return callable
64
     * @throws \BadMethodCallException
65
     */
66 1
	protected function getPoint(ServerRequestInterface $request) : callable
67
    {
68 1
        $controller = $this->getControllerClass($request);
69 1
        $this->checkController($controller);
70
71 1
        $controller = new $controller($request);
72
73 1
        $action = $this->getAction($request) ?? 'index';
74 1
        $this->checkAction($controller, $action);
75
76 1
        return [$controller, $action];
77
    }
78
79 2
    protected function getControllerClass(ServerRequestInterface $request): string
80
    {
81 2
        return $this->controller ?? '';
82
    }
83
84
	/**
85
	 * @param string $controller
86
	 * @throws \BadMethodCallException
87
	 */
88 2
	protected function checkController(string $controller) : void
89
    {
90 2
        if (!class_exists($controller)) {
91 1
            throw new \BadMethodCallException('Controller not found');
92
        }
93 1
    }
94
95
	/**
96
	 * @param \object $controller
97
	 * @param string $action
98
	 * @throws \BadMethodCallException
99
	 */
100 4
	protected function checkAction($controller, string $action) : void
101
    {
102 4
        if (!method_exists($controller, $action)) {
103 2
            throw new \BadMethodCallException('Action not found');
104
        }
105 2
    }
106
107 4
	protected function getAction(ServerRequestInterface $request): ?string
108
	{
109 4
        $route = $this->getRoute($request);
110 4
        $matches = $route->getMatchesParams();
111 4
		return $matches['_action'] ?? $this->action ?? null;
112
	}
113
114 1
	protected function getRoute(ServerRequestInterface $request): Route
115
    {
116 1
        return $request->getAttribute('route');
117
    }
118
}
119