1 | <?php |
||
2 | |||
3 | namespace Maestriam\FileSystem\Foundation; |
||
4 | |||
5 | use Illuminate\Support\Facades\Cache; |
||
6 | use Maestriam\FileSystem\Concerns\FluentGetter; |
||
7 | use Maestriam\FileSystem\Foundation\Drive\StructureDirectory; |
||
8 | |||
9 | class Drive |
||
10 | { |
||
11 | use FluentGetter; |
||
12 | |||
13 | /** |
||
14 | * Nome do driver que será manipulado |
||
15 | */ |
||
16 | private string $name; |
||
17 | |||
18 | /** |
||
19 | * Instância responsável pelas RNs sobre diretórios |
||
20 | */ |
||
21 | private StructureDirectory $structure; |
||
22 | |||
23 | /** |
||
24 | * Driver para manipulações de arquivos |
||
25 | * |
||
26 | * @param string $name |
||
27 | */ |
||
28 | public function __construct(string $name) |
||
29 | { |
||
30 | $this->initStructure()->setName($name); |
||
31 | |||
32 | if (! $this->exists()) { |
||
33 | return $this; |
||
34 | } |
||
35 | |||
36 | return $this->load(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Carrega as informações do drive salvas no cache |
||
41 | * |
||
42 | * @return Drive |
||
43 | */ |
||
44 | private function load() : Drive |
||
45 | { |
||
46 | $name = $this->getName(); |
||
47 | $cached = Cache::get($name); |
||
48 | |||
49 | $structure = $cached['structure']; |
||
50 | |||
51 | $this->setName($cached['name']); |
||
52 | |||
53 | $this->structure()->root($structure['root']); |
||
54 | $this->structure()->template($structure['template']); |
||
55 | $this->structure()->paths($structure['paths']); |
||
56 | |||
57 | return $this; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Cria instância para manipulações de diretórios |
||
62 | * |
||
63 | * @return Drive |
||
64 | */ |
||
65 | private function initStructure() : Drive |
||
66 | { |
||
67 | $this->structure = new StructureDirectory(); |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Define o nome do drive que será manipulado |
||
74 | * |
||
75 | * @param string $name |
||
76 | * @return Drive |
||
77 | */ |
||
78 | private function setName(string $name) : Drive |
||
79 | { |
||
80 | $this->name = $name; |
||
81 | |||
82 | return $this; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Retorna o nome do drive |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | private function getName() : string |
||
91 | { |
||
92 | return $this->name; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Retorna a instância para manipulação de arquivos baseado em um template, |
||
97 | * sobre as configurações deste driver |
||
98 | * |
||
99 | * @param string $name |
||
100 | * @return File |
||
101 | */ |
||
102 | public function template(string $name) : Template |
||
103 | { |
||
104 | return new Template($name, $this->structure); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Retorna a estrutura de diretórios defindos no drive |
||
109 | * |
||
110 | * @return StructureDirectory |
||
111 | */ |
||
112 | public function structure() : StructureDirectory |
||
113 | { |
||
114 | return $this->structure; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Converte as informações do objeto para um array |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | private function toArray() : array |
||
123 | { |
||
124 | return [ |
||
125 | 'name' => $this->name, |
||
126 | 'structure' => [ |
||
127 | 'root' => $this->structure->root(), |
||
128 | 'paths' => $this->structure->paths(), |
||
129 | 'template' => $this->structure->template(), |
||
130 | ] |
||
131 | ]; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Salva as informações definidas do drive no cache da aplicação |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | public function save() |
||
140 | { |
||
141 | Cache::add($this->name, $this->toArray()); |
||
142 | |||
143 | return $this; |
||
0 ignored issues
–
show
|
|||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Retorna se o drive existe no cache |
||
148 | * |
||
149 | * @return boolean |
||
150 | */ |
||
151 | public function exists() : bool |
||
152 | { |
||
153 | $name = $this->getName(); |
||
154 | |||
155 | $cached = Cache::get($name); |
||
156 | |||
157 | return (! $cached) ? false : true; |
||
158 | } |
||
159 | } |