Template::preview()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Maestriam\FileSystem\Foundation;
4
5
use Maestriam\FileSystem\Foundation\Drive\PathSanitizer;
6
use Maestriam\FileSystem\Foundation\Template\StubFile;
7
use Maestriam\FileSystem\Foundation\Drive\StructureDirectory;
8
use Maestriam\FileSystem\Foundation\File\FileInfo;
9
use Maestriam\FileSystem\Support\FileSystem;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Maestriam\FileSystem\Foundation\FileSystem. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
11
class Template
12
{
13
    /**
14
     * Nome do temmplate utilizado
15
     */
16
    private string $name;
17
18
    /**
19
     * Nome do arquivo stub
20
     */
21
    private StubFile $stub;
22
23
    private string $filename;
24
25
    private array $placeholders = [];
26
    
27
    /**
28
     * Regras de negócio de diretório 
29
     */
30
    private StructureDirectory $structure;
31
32
    /**
33
     * Regras de negócio para manipulação de arquivos baseado em template
34
     *
35
     * @param string $name
36
     * @param StructureDirectory $structure
37
     */
38
    public function __construct(string $name, StructureDirectory $structure)
39
    {
40
        $this->setStructure($structure)->loadStub($name)->setName($name);  
41
    }
42
43
    /**
44
     * Define a estrutura de diretórios utilizadas no template
45
     *
46
     * @param StructureDirectory $structure
47
     * @return Template
48
     */
49
    private function setStructure(StructureDirectory $structure) : Template
50
    {
51
        $this->structure = $structure;
52
53
        return $this;
54
    }
55
56
    /**
57
     * Carrega as informações de um arquivo stub
58
     *
59
     * @param string $name
60
     * @return Template
61
     */
62
    private function loadStub(string $name) : Template
63
    {
64
        $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...
65
66
        $this->stub = new StubFile($source, $name);
67
68
        return $this;
69
    }
70
    
71
    /**
72
     * Define o nome do arquivo de template
73
     *
74
     * @param string $name
75
     * @return Template
76
     */
77
    private function setName(string $name) : Template
78
    {
79
        $this->name = $name;
80
        
81
        return $this;
82
    }
83
84
    public function filename(string $filename) : Template
85
    {
86
        $this->filename = $filename;
87
88
        return $this;
89
    }
90
91
    public function placeholders(array $placeholders = []) : Template
92
    {
93
        $this->placeholders = $placeholders;
94
95
        return $this;
96
    }
97
98
99
    /**
100
     * Cria um arquivo baseado em um template
101
     *
102
     * @param string $filename
103
     * @param array $placeholders
104
     * @return void
105
     */
106
    public function create(string $filename = null, array $placeholders = null) : FileInfo
107
    {          
108
        $filename = $filename ?? $this->filename;
109
110
        $placeholders = $placeholders ?? $this->placeholders;
111
112
        $content = $this->stub->parse($placeholders);
113
114
        $folder = $this->structure->findByTemplate($this->name);
115
116
        $file = new File($filename);
117
                
118
        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...
119
    }
120
121
    /**
122
     * Retorna o conteúdo de um template já interpretado
123
     *
124
     * @param array $placeholders
125
     * @return string
126
     */
127
    public function preview(array $placeholders) : string
128
    {
129
        return $this->stub->parse($placeholders);
130
    }
131
132
    /**
133
     * Retorna se o arquivo, baseado em um modelo de template, 
134
     * já existe no diretório pré-definido.  
135
     *
136
     * @param array $placeholders
137
     * @return string
138
     */
139
    public function exists(string $filename) : bool
140
    {
141
        $file = $this->generateFilename($filename);
142
143
        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...
144
    }
145
146
    /**
147
     * Retorna o contéudo
148
     *
149
     * @param string $filename
150
     * @return string|null
151
     */
152
    public function load(string $filename) : ?string
153
    {
154
        if (! $this->exists($filename)) {
155
            return null;
156
        }
157
158
        $file = $this->generateFilename($filename);
159
160
        return file_get_contents($file);
161
    }
162
163
    /**
164
     * Retorna o caminho absoluto do arquivo, baseado nas premissas 
165
     * definidas do template.  
166
     *
167
     * @return string
168
     */
169
    public function path(string $filename) : string
170
    {
171
        return $this->generateFilename($filename);
172
    }
173
174
    private function generateFilename(string $filename) : string
175
    {
176
        $folder = $this->structure->findByTemplate($this->name);
177
178
        $path = $folder . DS . $filename; 
179
180
        return PathSanitizer::sanitize($path);
181
    }
182
}