Failed Conditions
Pull Request — master (#1)
by Yo
01:55
created

TemplateHelper::loadTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Yoanm\DefaultPhpRepository\Helper;
3
4
use Symfony\Component\Filesystem\Filesystem;
5
use Yoanm\DefaultPhpRepository\Exception\TargetFileExistsException;
6
7
/**
8
 * Class TemplateFileProcessor
9
 */
10
class TemplateHelper
11
{
12
    /** @var array */
13
    private $variableList = [];
14
    /** @var string[] */
15
    private $variableNameList = [];
16
    /** @var string */
17
    private $filenameResolverRegexp;
18
    /** @var Filesystem */
19
    private $fs;
20
21
    /**
22
     * @param array $variableList
23
     * @param array $extraTemplatePath
24
     */
25
    public function __construct(array $variableList, array $extraTemplatePath = [])
26
    {
27
        $this->variableList = $variableList;
28
        foreach($this->variableList as $variableId => $variableValue) {
29
            $variableId = sprintf('%%%s%%', $variableId);
30
            $this->variableNameList[$variableId] = $variableId;
31
        }
32
33
        foreach ($extraTemplatePath as $extraKey => $extraValue) {
34
            $variableId = sprintf('%%%s%%', $extraKey);
35
            $this->variableNameList[$variableId] = $variableId;
36
            $this->variableList[$variableId] = $this->loadTemplate($extraValue);
37
        }
38
39
        // compile this regexp at startup (no need to to it each time)
40
        $this->filenameResolverRegexp = sprintf(
41
            '#%s?(?:[^%s]+%s)*([^%s]+)\.tmpl$#',
42
            PathHelper::separator(),
43
            PathHelper::separator(),
44
            PathHelper::separator(),
45
            PathHelper::separator()
46
        );
47
        // compile this regexp at startup (no need to to it each time)
48
        $this->filePathResolverRegexp = sprintf(
0 ignored issues
show
Bug introduced by
The property filePathResolverRegexp does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
            '#%s?(?:[^%s]+%s)*[^%s]+\.tmpl$#',
50
            PathHelper::separator(),
51
            PathHelper::separator(),
52
            PathHelper::separator(),
53
            PathHelper::separator()
54
        );
55
56
57
        $this->fs = new Filesystem();
58
    }
59
60
    /**
61
     * @param string $templateFilePath
62
     * @param string $outputFilePath
63
     * @param bool   $overwrite overwrite even if present
64
     *
65
     * @throws TargetFileExistsException
66
     */
67
    public function dumpTemplate($templateFilePath, $outputFilePath, $overwrite = false)
0 ignored issues
show
Unused Code introduced by
The parameter $overwrite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
    {
69
        $this->fs->dumpFile($outputFilePath, $this->loadTemplate($templateFilePath));
70
    }
71
72
    /**
73
     * @param string $templateFilePath
74
     *
75
     * @return string
76
     */
77
    public function resolveOutputFilePath($templateFilePath, $outputDir)
78
    {
79
        return sprintf(
80
            '%s%s',
81
            PathHelper::appendPathSeparator($outputDir),
82
            $this->resolveOutputFilename($templateFilePath)
83
        );
84
    }
85
86
    /**
87
     * @param string $templateFilePath
88
     *
89
     * @return string
90
     */
91
    public function resolveOutputFilename($templateFilePath)
92
    {
93
        return preg_replace($this->filenameResolverRegexp, '\1', $templateFilePath);
94
    }
95
96
    /**
97
     * @param string $templateFilePath
98
     *
99
     * @return string file content
100
     */
101
    public function loadTemplate($templateFilePath)
102
    {
103
        return str_replace($this->variableNameList, $this->variableList, file_get_contents($templateFilePath));
104
    }
105
}
106