Passed
Push — develop ( e32fe8...a6721e )
by Brent
02:31
created

PartialParse::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

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