Passed
Push — master ( ec2490...6f4616 )
by Mike
03:21
created

ExecutionProvider::getPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Xervice\Web\Business\Executor;
5
6
7
use Xervice\Routing\RoutingFacade;
8
use Xervice\Web\Business\Exception\WebExeption;
9
use Xervice\Web\Business\Executor\ResponseHandler\ResponseHandlerInterface;
10
11
class ExecutionProvider implements ExecutionProviderInterface
12
{
13
    /**
14
     * @var \Xervice\Routing\RoutingFacade
15
     */
16
    private $routeFacade;
17
18
    /**
19
     * @var \Xervice\Web\Business\Executor\ResponseHandler\ResponseHandlerInterface
20
     */
21
    private $responseHandler;
22
23
    /**
24
     * ExecutionProvider constructor.
25
     *
26
     * @param \Xervice\Routing\RoutingFacade $routeFacade
27
     * @param \Xervice\Web\Business\Executor\ResponseHandler\ResponseHandlerInterface $responseHandler
28
     */
29 2
    public function __construct(
30
        RoutingFacade $routeFacade,
31
        ResponseHandlerInterface $responseHandler
32
    ) {
33 2
        $this->routeFacade = $routeFacade;
34 2
        $this->responseHandler = $responseHandler;
35 2
    }
36
37
    /**
38
     * @throws \Xervice\Web\Business\Exception\WebExeption
39
     */
40
    public function execute(): void
41
    {
42
        $this->executeUrl(
43
            sprintf(
44
                '%s',
45
                $this->getPath()
46
            )
47
        );
48
    }
49
50
    /**
51
     * @param string $url
52
     *
53
     * @throws \Xervice\Web\Business\Exception\WebExeption
54
     */
55 2
    public function executeUrl(string $url): void
56
    {
57 2
        $executionData = $this->routeFacade->matchUrl($url);
58 2
        $this->validateExecutionData($executionData);
59 2
        $this->responseHandler->handleResponse($executionData);
60 2
    }
61
62
    /**
63
     * @param array $executionData
64
     *
65
     * @throws \Xervice\Web\Business\Exception\WebExeption
66
     */
67 2
    private function validateExecutionData(array $executionData): void
68
    {
69 2
        if (!isset($executionData['_controller'])) {
70
            throw new WebExeption(
71
                sprintf(
72
                    'No callable given for route %s',
73
                    $executionData['_route']
74
                )
75
            );
76
        }
77 2
    }
78
79
    /**
80
     * @return string
81
     */
82
    private function getPath(): string
83
    {
84
        $path = '/';
85
        if (isset($_SERVER['REQUEST_URI'])) {
86
            $path = parse_url($_SERVER['REQUEST_URI']);
87
            $path = $path['path'] ?? '/';
88
        }
89
90
        return $path;
91
    }
92
}