DynamicPoint::getControllerClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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