Passed
Push — master ( fec7a7...e6f852 )
by Giuliano
03:19
created

Template::filename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Maestriam\FileSystem\Foundation;
4
5
use Maestriam\FileSystem\Foundation\Template\StubFile;
6
use Maestriam\FileSystem\Foundation\Drive\StructureDirectory;
7
use Maestriam\FileSystem\Foundation\File\FileInfo;
8
9
class Template
10
{
11
    /**
12
     * Nome do temmplate utilizado
13
     */
14
    private string $name;
15
16
    /**
17
     * Nome do arquivo stub
18
     */
19
    private StubFile $stub;
20
21
    private string $filename;
22
23
    private array $placeholders = [];
24
    
25
    /**
26
     * Regras de negócio de diretório 
27
     */
28
    private StructureDirectory $structure;
29
30
    /**
31
     * Regras de negócio para manipulação de arquivos baseado em template
32
     *
33
     * @param string $name
34
     * @param StructureDirectory $structure
35
     */
36
    public function __construct(string $name, StructureDirectory $structure)
37
    {
38
        $this->setStructure($structure)->loadStub($name)->setName($name);  
39
    }
40
41
    /**
42
     * Define a estrutura de diretórios utilizadas no template
43
     *
44
     * @param StructureDirectory $structure
45
     * @return Template
46
     */
47
    private function setStructure(StructureDirectory $structure) : Template
48
    {
49
        $this->structure = $structure;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Carrega as informações de um arquivo stub
56
     *
57
     * @param string $name
58
     * @return Template
59
     */
60
    private function loadStub(string $name) : Template
61
    {
62
        $source = $this->structure->template;
0 ignored issues
show
Bug Best Practice introduced by
The property template does not exist on Maestriam\FileSystem\Fou...rive\StructureDirectory. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
64
        $this->stub = new StubFile($source, $name);
65
66
        return $this;
67
    }
68
    
69
    /**
70
     * Define o nome do arquivo de template
71
     *
72
     * @param string $name
73
     * @return Template
74
     */
75
    private function setName(string $name) : Template
76
    {
77
        $this->name = $name;
78
        
79
        return $this;
80
    }
81
82
    public function filename(string $filename) : Template
83
    {
84
        $this->filename = $filename;
85
86
        return $this;
87
    }
88
89
    public function placeholders(array $placeholders = []) : Template
90
    {
91
        $this->placeholders = $placeholders;
92
93
        return $this;
94
    }
95
96
97
    /**
98
     * Cria um arquivo baseado em um template
99
     *
100
     * @param string $filename
101
     * @param array $placeholders
102
     * @return void
103
     */
104
    public function create(string $filename = null, array $placeholders = null) : FileInfo
105
    {          
106
        $filename = $filename ?? $this->filename;
107
108
        $placeholders = $placeholders ?? $this->placeholders;
109
110
        $content = $this->stub->parse($placeholders);
111
112
        $folder = $this->structure->findByTemplate($this->name);
113
114
        $file = new File($filename);
115
                
116
        return $file->setFolder($folder)->create($content);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $file->setFolder(...lder)->create($content) returns the type Maestriam\FileSystem\Foundation\File\FileInfo which is incompatible with the documented return type void.
Loading history...
117
    }
118
119
    /**
120
     * Retorna o conteúdo de um template já interpretado
121
     *
122
     * @param array $placeholders
123
     * @return string
124
     */
125
    public function preview(array $placeholders) : string
126
    {
127
        return $this->stub->parse($placeholders);
128
    }
129
130
    /**
131
     * Retorna o arquivo, baseado em um modelo de template,
132
     * já existe no diretório definido
133
     *
134
     * @param array $placeholders
135
     * @return string
136
     */
137
    public function exists(string $filename) : bool
138
    {
139
        $file = $this->generateFilename($filename);
140
141
        return (is_file($file)) ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return is_file($file) ? true : false returns the type boolean which is incompatible with the documented return type string.
Loading history...
142
    }
143
144
    public function load(string $filename) : ?string
145
    {
146
        if (! $this->exists($filename)) {
147
            return null;
148
        }
149
150
        $file = $this->generateFilename($filename);
151
152
        return file_get_contents($file);
153
    }
154
155
    private function generateFilename(string $filename) : string
156
    {
157
        $folder = $this->structure->findByTemplate($this->name);
158
159
        return $folder . DS . $filename;
160
    }
161
}