TemplateGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 58
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A compile() 0 12 1
A makeDirectory() 0 6 2
1
<?php
2
3
namespace Te7aHoudini\Laroute\Generators;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Te7aHoudini\Laroute\Compilers\CompilerInterface as Compiler;
7
8
class TemplateGenerator implements GeneratorInterface
9
{
10
    /**
11
     * The compiler instance.
12
     *
13
     * @var \Te7aHoudini\Laroute\Compilers\CompilerInterface
14
     */
15
    protected $compiler;
16
17
    /**
18
     * The filesystem instance.
19
     *
20
     * @var \Illuminate\Filesystem\Filesystem
21
     */
22
    protected $filesystem;
23
24
    /**
25
     * Create a new template generator instance.
26
     *
27
     * @param $compiler   \Te7aHoudini\Laroute\Compilers\CompilerInterface
28
     * @param $filesystem \Illuminate\Filesystem\Filesystem
29
     */
30
    public function __construct(Compiler $compiler, Filesystem $filesystem)
31
    {
32
        $this->compiler = $compiler;
33
34
        $this->filesystem = $filesystem;
35
    }
36
37
    /**
38
     * Compile the template.
39
     *
40
     * @param $templatePath
41
     * @param $templateData
42
     * @param $filePath
43
     *
44
     * @return string
45
     */
46
    public function compile($templatePath, array $templateData, $filePath)
47
    {
48
        $template = $this->filesystem->get($templatePath);
49
50
        $compiled = $this->compiler->compile($template, $templateData);
51
52
        $this->makeDirectory(dirname($filePath));
53
54
        $this->filesystem->put($filePath, $compiled);
55
56
        return $filePath;
57
    }
58
59
    public function makeDirectory($directory)
60
    {
61
        if (! $this->filesystem->isDirectory($directory)) {
62
            $this->filesystem->makeDirectory($directory, 0777, true);
63
        }
64
    }
65
}
66