Generator::compile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php namespace Way\Generators;
2
3
use Illuminate\Contracts\Filesystem\FileExistsException;
4
use Illuminate\Support\Facades\File;
5
use Way\Generators\Compilers\TemplateCompiler;
6
7
class Generator
8
{
9
    /**
10
     * Run the generator
11
     *
12
     * @param  string  $templatePath
13
     * @param  array  $templateData
14
     * @param  string  $filePathToGenerate
15
     * @throws FileExistsException
16
     */
17
    public function make(string $templatePath, array $templateData, string $filePathToGenerate)
18
    {
19
        // We first need to compile the template,
20
        // according to the data that we provide.
21
        $template = $this->compile($templatePath, $templateData, new TemplateCompiler);
22
23
        // Now that we have the compiled template,
24
        // we can actually generate the file.
25
        if (File::exists($filePathToGenerate)) {
26
            throw new FileExistsException();
27
        }
28
29
        File::put($filePathToGenerate, $template);
30
    }
31
32
    /**
33
     * Compile the file
34
     *
35
     * @param  string  $templatePath
36
     * @param  array  $data
37
     * @param  TemplateCompiler  $compiler
38
     * @return mixed
39
     */
40
    public function compile(string $templatePath, array $data, TemplateCompiler $compiler)
41
    {
42
        return $compiler->compile(File::get($templatePath), $data);
43
    }
44
}
45