Passed
Branch feature/first-implementation (72fcf8)
by Andrea Marco
04:38
created

Manifest::getUseStatements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Cerbero\LaravelDto\Console;
4
5
use Illuminate\Support\Arr;
6
7
/**
8
 * The DTOs generation manifest.
9
 *
10
 */
11
class Manifest
12
{
13
    /**
14
     * The manifest filename.
15
     *
16
     * @var string
17
     */
18
    protected $filename;
19
20
    /**
21
     * The manifest content.
22
     *
23
     * @var array
24
     */
25
    protected $manifest = [];
26
27
    /**
28
     * Instantiate the class.
29
     *
30
     * @param string $filename
31
     */
32
    public function __construct(string $filename)
33
    {
34
        $this->filename = $filename;
35
    }
36
37
    /**
38
     * Write content in the manifest
39
     *
40
     * @param string $key
41
     * @param mixed $value
42
     * @return self
43
     */
44
    public function write(string $key, $value): self
45
    {
46
        $this->manifest = $this->read();
47
48
        Arr::set($this->manifest, $key, $value);
49
50
        return $this;
51
    }
52
53
    /**
54
     * Retrieve the manifest content
55
     *
56
     * @param string|null $key
57
     * @param mixed $default
58
     * @return mixed
59
     */
60
    public function read(string $key = null, $default = null)
61
    {
62
        if (!file_exists($this->filename)) {
63
            file_put_contents($this->filename, '<?php return [];');
64
        }
65
66
        if (empty($this->manifest)) {
67
            $this->manifest = require $this->filename;
68
        }
69
70
        return $key === null ? $this->manifest : Arr::get($this->manifest, $key, $default);
71
    }
72
73
    /**
74
     * Save the manifest
75
     *
76
     * @return self
77
     */
78
    public function save(): self
79
    {
80
        file_put_contents($this->filename, '<?php return ' . var_export($this->read(), true) . ';');
81
82
        return $this;
83
    }
84
85
    /**
86
     * Delete the manifest
87
     *
88
     * @return void
89
     */
90
    public function delete(): void
91
    {
92
        if (file_exists($this->filename)) {
93
            unlink($this->filename);
94
        }
95
    }
96
97
    /**
98
     * Add the given model DTO
99
     *
100
     * @param string $model
101
     * @param string $dto
102
     * @return self
103
     */
104
    public function addDto(string $model, string $dto): self
105
    {
106
        $this->write("{$model}.dto", $dto);
107
108
        return $this;
109
    }
110
111
    /**
112
     * Retrieve the given model DTO
113
     *
114
     * @param string $model
115
     * @return string|null
116
     */
117
    public function getDto(string $model): ?string
118
    {
119
        return $this->read("{$model}.dto");
120
    }
121
122
    /**
123
     * Add the given model use statements
124
     *
125
     * @param string $model
126
     * @param string $name
127
     * @param string $use
128
     * @return self
129
     */
130
    public function addUseStatement(string $model, string $name, string $use): self
131
    {
132
        $this->write("{$model}.use.{$name}", $use);
133
134
        return $this;
135
    }
136
137
    /**
138
     * Retrieve the given model use statements
139
     *
140
     * @param string $model
141
     * @return array
142
     */
143
    public function getUseStatements(string $model): array
144
    {
145
        return $this->read("{$model}.use", []);
146
    }
147
148
    /**
149
     * Add the given DTO that is being generated
150
     *
151
     * @param string $dto
152
     * @return self
153
     */
154
    public function addGeneratingDto(string $dto): self
155
    {
156
        $this->write("generating.{$dto}", true);
157
158
        return $this;
159
    }
160
161
    /**
162
     * Retrieve the DTO that is being generated now
163
     *
164
     * @return string|null
165
     */
166
    public function getGeneratingDto(): ?string
167
    {
168
        if (empty($generating = $this->read('generating'))) {
169
            return null;
170
        }
171
172
        $dtos = array_keys($generating);
173
174
        return end($dtos);
175
    }
176
177
    /**
178
     * Determine whether the given DTO is being generated now
179
     *
180
     * @param string $dto
181
     * @return bool
182
     */
183
    public function isGenerating(string $dto): bool
184
    {
185
        return $this->getGeneratingDto() === $dto;
186
    }
187
188
    /**
189
     * Determine whether the given DTO is in the process of being generated
190
     *
191
     * @param string $dto
192
     * @return bool
193
     */
194
    public function generating(string $dto): bool
195
    {
196
        return $this->read("generating.{$dto}") !== null;
197
    }
198
199
    /**
200
     * Mark the latest generating DTO as generated
201
     *
202
     * @return self
203
     */
204
    public function finishGeneratingDto(): self
205
    {
206
        if (!empty($this->read('generating'))) {
207
            $dto = $this->getGeneratingDto();
208
            $this->write("generated.{$dto}", true);
209
            array_pop($this->manifest['generating']);
210
        }
211
212
        return $this;
213
    }
214
215
    /**
216
     * Determine whether the given DTO has been generated
217
     *
218
     * @param string $dto
219
     * @return bool
220
     */
221
    public function generated(string $dto): bool
222
    {
223
        return $this->read("generated.{$dto}") !== null;
224
    }
225
226
    /**
227
     * Retrieve the DTOs that have been generated
228
     *
229
     * @return array
230
     */
231
    public function getGenerated(): array
232
    {
233
        if (empty($generated = $this->read('generated'))) {
234
            return [];
235
        }
236
237
        return array_keys($generated);
238
    }
239
240
    /**
241
     * Add the given class as starting DTO
242
     *
243
     * @param string $dto
244
     * @return self
245
     */
246
    public function addStartingDto(string $dto): self
247
    {
248
        if ($this->read('starting') === null) {
249
            $this->write('starting', $dto);
250
        }
251
252
        return $this;
253
    }
254
255
    /**
256
     * Retrieve the starting DTO
257
     *
258
     * @return string|null
259
     */
260
    public function getStartingDto(): ?string
261
    {
262
        return $this->read('starting');
263
    }
264
265
    /**
266
     * Determine whether the given class is the starting DTO
267
     *
268
     * @param string $dto
269
     * @return bool
270
     */
271
    public function isStartingDto(string $dto): bool
272
    {
273
        return $this->getStartingDto() === $dto;
274
    }
275
}
276