Completed
Push — master ( c56f94...60d510 )
by Tomáš
9s
created

setConfigurationToFileIfFoundAny()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Renderable\Configuration;
11
12
use Symplify\PHP7_Sculpin\Configuration\Parser\YamlAndNeonParser;
13
use Symplify\PHP7_Sculpin\Renderable\File\File;
14
15
final class ConfigurationDecorator
16
{
17
    /**
18
     * @var YamlAndNeonParser
19
     */
20
    private $yamlAndNeonParser;
21
22 7
    public function __construct(YamlAndNeonParser $yamlAndNeonParser)
23
    {
24 7
        $this->yamlAndNeonParser = $yamlAndNeonParser;
25 7
    }
26
27 7
    public function decorateFile(File $file)
28
    {
29 7
        if (preg_match('/^\s*(?:---[\s]*[\r\n]+)(.*?)(?:---[\s]*[\r\n]+)(.*?)$/s', $file->getContent(), $matches)) {
30 4
            $file->changeContent($matches[2]);
31
32 4
            $this->setConfigurationToFileIfFoundAny($matches[1], $file);
33
        }
34 7
    }
35
36 4
    private function setConfigurationToFileIfFoundAny(string $content, File $file)
37
    {
38 4
        if (!preg_match('/^(\s*[-]+\s*|\s*)$/', $content)) {
39 4
            $configuration = $this->yamlAndNeonParser->decode($content);
40 4
            $file->setConfiguration($configuration);
41
        }
42 4
    }
43
}
44