PartialParse::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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