Manifest::addStartingDto()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
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 10
    public function __construct(string $filename)
33
    {
34 10
        $this->filename = $filename;
35 10
    }
36
37
    /**
38
     * Write content in the manifest
39
     *
40
     * @param string $key
41
     * @param mixed $value
42
     * @return self
43
     */
44 3
    public function write(string $key, $value): self
45
    {
46 3
        $this->manifest = $this->read();
47
48 3
        Arr::set($this->manifest, $key, $value);
49
50 3
        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 4
    public function read(string $key = null, $default = null)
61
    {
62 4
        if (!file_exists($this->filename)) {
63 4
            file_put_contents($this->filename, '<?php return [];');
64
        }
65
66 4
        if (empty($this->manifest)) {
67 4
            $this->manifest = require $this->filename;
68
        }
69
70 4
        return $key === null ? $this->manifest : Arr::get($this->manifest, $key, $default);
71
    }
72
73
    /**
74
     * Save the manifest
75
     *
76
     * @return self
77
     */
78 3
    public function save(): self
79
    {
80 3
        file_put_contents($this->filename, '<?php return ' . var_export($this->read(), true) . ';');
81
82 3
        return $this;
83
    }
84
85
    /**
86
     * Delete the manifest
87
     *
88
     * @return void
89
     */
90 4
    public function delete(): void
91
    {
92 4
        if (file_exists($this->filename)) {
93 4
            unlink($this->filename);
94
        }
95 4
    }
96
97
    /**
98
     * Add the given model DTO
99
     *
100
     * @param string $model
101
     * @param string $dto
102
     * @return self
103
     */
104 2
    public function addDto(string $model, string $dto): self
105
    {
106 2
        $this->write("{$model}.dto", $dto);
107
108 2
        return $this;
109
    }
110
111
    /**
112
     * Retrieve the given model DTO
113
     *
114
     * @param string $model
115
     * @return string|null
116
     */
117 2
    public function getDto(string $model): ?string
118
    {
119 2
        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 3
    public function addUseStatement(string $model, string $name, string $use): self
131
    {
132 3
        $this->write("{$model}.use.{$name}", $use);
133
134 3
        return $this;
135
    }
136
137
    /**
138
     * Retrieve the given model use statements
139
     *
140
     * @param string $model
141
     * @return array
142
     */
143 3
    public function getUseStatements(string $model): array
144
    {
145 3
        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 2
    public function addGeneratingDto(string $dto): self
155
    {
156 2
        $this->write("generating.{$dto}", true);
157
158 2
        return $this;
159
    }
160
161
    /**
162
     * Retrieve the DTO that is being generated now
163
     *
164
     * @return string|null
165
     */
166 3
    public function getGeneratingDto(): ?string
167
    {
168 3
        if (empty($generating = $this->read('generating'))) {
169 1
            return null;
170
        }
171
172 2
        $dtos = array_keys($generating);
173
174 2
        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 1
    public function isGenerating(string $dto): bool
184
    {
185 1
        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 2
    public function generating(string $dto): bool
195
    {
196 2
        return $this->read("generating.{$dto}") !== null;
197
    }
198
199
    /**
200
     * Mark the latest generating DTO as generated
201
     *
202
     * @return self
203
     */
204 2
    public function finishGeneratingDto(): self
205
    {
206 2
        if (!empty($this->read('generating'))) {
207 2
            $dto = $this->getGeneratingDto();
208 2
            $this->write("generated.{$dto}", true);
209 2
            array_pop($this->manifest['generating']);
210
        }
211
212 2
        return $this;
213
    }
214
215
    /**
216
     * Determine whether the given DTO has been generated
217
     *
218
     * @param string $dto
219
     * @return bool
220
     */
221 2
    public function generated(string $dto): bool
222
    {
223 2
        return $this->read("generated.{$dto}") !== null;
224
    }
225
226
    /**
227
     * Add the given class as starting DTO
228
     *
229
     * @param string $dto
230
     * @return self
231
     */
232 2
    public function addStartingDto(string $dto): self
233
    {
234 2
        if ($this->read('starting') === null) {
235 2
            $this->write('starting', $dto);
236
        }
237
238 2
        return $this;
239
    }
240
241
    /**
242
     * Retrieve the starting DTO
243
     *
244
     * @return string|null
245
     */
246 2
    public function getStartingDto(): ?string
247
    {
248 2
        return $this->read('starting');
249
    }
250
251
    /**
252
     * Determine whether the given class is the starting DTO
253
     *
254
     * @param string $dto
255
     * @return bool
256
     */
257 2
    public function isStartingDto(string $dto): bool
258
    {
259 2
        return $this->getStartingDto() === $dto;
260
    }
261
}
262