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

ExecutionProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 80
ccs 12
cts 28
cp 0.4286
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A executeUrl() 0 5 1
A execute() 0 6 1
A validateExecutionData() 0 7 2
A getPath() 0 9 2
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
}