Passed
Push — v2 ( 5c6a17...17236d )
by Brent
05:18
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\Command;
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 3
    public function setFilter(string $filter): PartialParse
17
    {
18 3
        $this->filter = $filter;
19
20 3
        return $this;
21
    }
22
23 3
    public function execute(): void
24
    {
25 3
        $parsedConfiguration = Yaml::parse(File::read($this->configurationFile));
26
27 3
        $routeCollection = $this->createRouteCollection($parsedConfiguration);
28 3
        $matcher = new UrlMatcher($routeCollection, new RequestContext());
29 3
        $matchingRoute = $matcher->match($this->filter);
30
31 3
        $filteredConfiguration = array_filter($parsedConfiguration, function ($key) use ($matchingRoute) {
32 3
            return $key === $matchingRoute['_route'];
33 3
        }, ARRAY_FILTER_USE_KEY);
34
35 3
        $pages = $this->parsePageConfiguration($filteredConfiguration);
36
37 3
        $this->renderPages($pages);
38 3
    }
39
40 3
    private function createRouteCollection(array $configuration): RouteCollection
41
    {
42 3
        $routeCollection = new RouteCollection();
43
44 3
        foreach ($configuration as $id => $pageConfiguration) {
45 3
            $routeCollection->add($id, new Route($id));
46
        }
47
48 3
        return $routeCollection;
49
    }
50
}
51