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

DynamicPoint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 28
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getControllerClass() 0 11 2
A normalizeClassFromUrl() 0 6 1
A getAction() 0 4 1
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