Completed
Push — master ( 06321c...b705c4 )
by Alexpts
02:11
created

DynamicPoint::getControllerClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\EndPoint;
5
6
use Psr\Http\Message\ServerRequestInterface;
7
8
class DynamicPoint extends EndPoint
9
{
10
    protected $prefix = '';
11
12
    protected function getControllerClass(ServerRequestInterface $request) : string
13
    {
14
        $route = $this->getRoute($request);
15
        $matches = $route->getMatchesParams();
16
17
        if (!array_key_exists('_controller', $matches)) {
18
            throw new \BadMethodCallException('Not found controller name for dynamic controller point');
19
        }
20
21
        return $this->prefix . $this->normalizeClassFromUrl($matches['_controller']);
22
    }
23
24
    protected function normalizeClassFromUrl(string $class) : string
25
    {
26
        return array_reduce(explode('-', $class), function ($prev, $item) {
27
            return $prev . ucfirst($item);
28
        });
29
    }
30
31
    protected function getAction(ServerRequestInterface $request): ?string
32
    {
33
        return parent::getAction($request) ?? strtolower($request->getMethod());
34
    }
35
}
36