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

PartialParse::createRouteCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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