Passed
Push — develop ( 3028ee...99cd1a )
by Brent
06:58
created

PartialParse   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setFilter() 0 6 1
A execute() 0 16 1
A createRouteCollection() 0 10 2
1
<?php
2
3
namespace Stitcher\Task;
4
5
use Stitcher\File;
6
use Symfony\Component\Routing\Matcher\UrlMatcher;
7
use Symfony\Component\Routing\RequestContext;
8
use Symfony\Component\Routing\Route;
9
use Symfony\Component\Routing\RouteCollection;
10
use Symfony\Component\Yaml\Yaml;
11
12
class PartialParse extends AbstractParse
13
{
14
    private $filter;
15
16 5
    public function setFilter(string $filter): PartialParse
17
    {
18 5
        $this->filter = $filter;
19
20 5
        return $this;
21
    }
22
23 5
    public function execute(): void
24
    {
25 5
        $parsedConfiguration = (array) Yaml::parse(File::read($this->configurationFile));
26
27 5
        $routeCollection = $this->createRouteCollection($parsedConfiguration);
28 5
        $matcher = new UrlMatcher($routeCollection, new RequestContext());
29 5
        $matchingRoute = $matcher->match($this->filter);
30
31 5
        $filteredConfiguration = array_filter($parsedConfiguration, function ($key) use ($matchingRoute) {
32 5
            return $key === $matchingRoute['_route'];
33 5
        }, ARRAY_FILTER_USE_KEY);
34
35 5
        $pages = $this->parsePageConfiguration($filteredConfiguration);
36
37 5
        $this->renderPages($pages);
38 5
    }
39
40 5
    private function createRouteCollection(array $configuration): RouteCollection
41
    {
42 5
        $routeCollection = new RouteCollection();
43
44 5
        foreach ($configuration as $id => $pageConfiguration) {
45 5
            $routeCollection->add($id, new Route($id));
46
        }
47
48 5
        return $routeCollection;
49
    }
50
}
51