Completed
Pull Request — master (#20)
by Pavel
02:51
created

CodeGenerate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 41
rs 10
c 1
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCode() 0 13 2
A getPath() 0 9 2
1
<?php
2
3
namespace App\Console\Traits;
4
5
trait CodeGenerate
6
{
7
    /**
8
     * Generate code
9
     *
10
     * @param array  $placeHolders
11
     * @param array  $replacements
12
     * @param string $templateName
13
     * @param string $resultPath
14
     * @return bool|int
15
     */
16
    public function generateCode($placeHolders, $replacements, $templateName, $resultPath)
17
    {
18
        $templatePath = CODE_TEMPLATE_PATH.'/'.$templateName;
19
        if (false === file_exists($templatePath)) {
20
            throw new \RunTimeException(sprintf('Not found template %s', $templatePath));
21
        }
22
23
        $template = file_get_contents($templatePath);
24
25
        $code = str_replace($placeHolders, $replacements, $template);
26
27
        return file_put_contents($resultPath, $code);
28
    }
29
30
    /**
31
     * @param string $baseName
32
     * @param string $dir
33
     * @return string
34
     * @throws \Exception
35
     */
36
    private function getPath($baseName, $dir)
37
    {
38
        $dir = rtrim($dir, '/');
39
        if (!file_exists($dir)) {
40
            throw new \Exception(sprintf('Commands directory "%s" does not exist.', $dir));
41
        }
42
43
        return $dir.'/'.$baseName;
44
    }
45
}
46