Passed
Push — develop ( 37d230...30ebd1 )
by Brent
03:14
created

PartialParse::execute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 25
ccs 15
cts 15
cp 1
crap 1
rs 8.8571
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 Symfony\Component\Routing\Matcher\UrlMatcher;
9
use Symfony\Component\Routing\RequestContext;
10
use Symfony\Component\Routing\Route;
11
use Symfony\Component\Routing\RouteCollection;
12
use Symfony\Component\Yaml\Yaml;
13
14
class PartialParse extends AbstractParse
15
{
16
    private $filter;
17
18 5
    public function setFilter(string $filter): PartialParse
19
    {
20 5
        $this->filter = $filter;
21
22 5
        return $this;
23
    }
24
25 5
    public function execute(): void
26
    {
27 5
        $parsedConfiguration = $this->getParsedConfiguration();
28
29 5
        $routeCollection = $this->createRouteCollection($parsedConfiguration);
30
31 5
        $matcher = new UrlMatcher($routeCollection, new RequestContext());
32
33 5
        $matchingRoute = $matcher->match($this->filter);
34
35 5
        CollectionAdapter::setFilterId($matchingRoute['id'] ?? null);
36
37 5
        $filteredConfiguration = array_filter(
38 5
            $parsedConfiguration,
39 5
            function ($key) use ($matchingRoute) {
40 5
                return $key === $matchingRoute['_route'];
41 5
            }, ARRAY_FILTER_USE_KEY
42
        );
43
44 5
        $pages = $this->parsePageConfiguration($filteredConfiguration);
45
46 5
        $this->renderPages($pages);
47
48 5
        $this->executeSubTasks();
49 5
    }
50
51 5
    private function createRouteCollection(array $configuration): RouteCollection
52
    {
53 5
        $routeCollection = new RouteCollection();
54
55 5
        foreach ($configuration as $id => $pageConfiguration) {
56 5
            $routeCollection->add($id, new Route($id));
57
        }
58
59 5
        return $routeCollection;
60
    }
61
62 5
    private function getParsedConfiguration(): array
63
    {
64 5
        $configurationFile = File::read($this->configurationFile);
65
66 5
        if (! $configurationFile) {
67
            throw InvalidConfiguration::siteConfigurationFileNotFound();
68
        }
69
70 5
        $parsedConfiguration = (array) Yaml::parse($configurationFile);
71
72 5
        return $parsedConfiguration;
73
    }
74
}
75