Passed
Push — develop ( 359c84...5df010 )
by Brent
04:09
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\Exception\InvalidConfiguration;
6
use Stitcher\File;
7
use Stitcher\Page\Adapter\CollectionAdapter;
8
use Stitcher\Page\Adapter\CollectionFilter;
9
use Symfony\Component\Routing\Matcher\UrlMatcher;
10
use Symfony\Component\Routing\RequestContext;
11
use Symfony\Component\Routing\Route;
12
use Symfony\Component\Routing\RouteCollection;
13
use Symfony\Component\Yaml\Yaml;
14
15
class PartialParse extends AbstractParse
16
{
17
    private $filter;
18
19 5
    public function setFilter(string $filter): PartialParse
20
    {
21 5
        $this->filter = $filter;
22
23 5
        return $this;
24
    }
25
26 5
    public function execute(): void
27
    {
28 5
        $parsedConfiguration = $this->getParsedConfiguration();
29
30 5
        $routeCollection = $this->createRouteCollection($parsedConfiguration);
31 5
        $matcher = new UrlMatcher($routeCollection, new RequestContext());
32 5
        $matchingRoute = $matcher->match($this->filter);
33
34 5
        CollectionAdapter::setFilterId($matchingRoute['id'] ?? null);
35
36 5
        $filteredConfiguration = array_filter($parsedConfiguration, function ($key) use ($matchingRoute) {
37 5
            return $key === $matchingRoute['_route'];
38 5
        }, ARRAY_FILTER_USE_KEY);
39
40 5
        $pages = $this->parsePageConfiguration($filteredConfiguration);
41
42 5
        $this->renderPages($pages);
43
44 5
        $this->executeSubTasks();
45 5
    }
46
47 5
    private function createRouteCollection(array $configuration): RouteCollection
48
    {
49 5
        $routeCollection = new RouteCollection();
50
51 5
        foreach ($configuration as $id => $pageConfiguration) {
52 5
            $routeCollection->add($id, new Route($id));
53
        }
54
55 5
        return $routeCollection;
56
    }
57
58 5
    private function getParsedConfiguration(): array
59
    {
60 5
        $configurationFile = File::read($this->configurationFile);
61
62 5
        if (!$configurationFile) {
63
            throw InvalidConfiguration::siteConfigurationFileNotFound();
64
        }
65
66 5
        $parsedConfiguration = (array) Yaml::parse($configurationFile);
67
68 5
        return $parsedConfiguration;
69
    }
70
}
71