Passed
Push — master ( 48aa9a...5ad1d2 )
by Richard
10:34
created

RepositoryGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PWWEB\Artomator\Generators;
4
5
use InfyOm\Generator\Common\CommandData;
6
use InfyOm\Generator\Generators\BaseGenerator;
7
use InfyOm\Generator\Utils\FileUtil;
8
9
class RepositoryGenerator extends BaseGenerator
10
{
11
    /** @var CommandData */
12
    private $commandData;
13
14
    /** @var string */
15
    private $path;
16
17
    /** @var string */
18
    private $fileName;
19
20
    public function __construct(CommandData $commandData)
21
    {
22
        $this->commandData = $commandData;
23
        $this->path = $commandData->config->pathRepository;
24
        $this->fileName = $this->commandData->modelName.'Repository.php';
25
    }
26
27
    public function generate()
28
    {
29
        $templateData = get_artomator_template('repository');
30
31
        $templateData = fill_template($this->commandData->dynamicVars, $templateData);
32
33
        $searchables = [];
34
35
        foreach ($this->commandData->fields as $field) {
36
            if ($field->isSearchable) {
37
                $searchables[] = "'".$field->name."'";
38
            }
39
        }
40
41
        $templateData = str_replace('$FIELDS$', implode(','.infy_nl_tab(1, 2), $searchables), $templateData);
42
43
        $docsTemplate = get_artomator_template('docs.repository');
44
        $docsTemplate = fill_template($this->commandData->dynamicVars, $docsTemplate);
45
        $docsTemplate = str_replace('$GENERATE_DATE$', date('F j, Y, g:i a T'), $docsTemplate);
46
47
        $templateData = str_replace('$DOCS$', $docsTemplate, $templateData);
48
49
        FileUtil::createFile($this->path, $this->fileName, $templateData);
50
51
        $this->commandData->commandComment("\nRepository created: ");
52
        $this->commandData->commandInfo($this->fileName);
53
    }
54
55
    public function rollback()
56
    {
57
        if ($this->rollbackFile($this->path, $this->fileName)) {
58
            $this->commandData->commandComment('Repository file deleted: '.$this->fileName);
59
        }
60
    }
61
}
62