Passed
Branch master (267be1)
by Eugene
03:26
created

ResourceDOAbstract::getUuid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
namespace Staticus\Resources;
3
4
use Zend\Permissions\Acl\Resource\ResourceInterface;
5
6
/**
7
 * Domain Object
8
 * @package Staticus\Resources\File
9
 */
10
abstract class ResourceDOAbstract implements ResourceDOInterface, \Iterator, ResourceInterface
11
{
12
    const TYPE = '';
13
    const TOKEN_BASEDIRECTORY = 'basedirectory';
14
    const TOKEN_NAMESPACE = 'namespace';
15
    const TOKEN_TYPE = 'type';
16
    const TOKEN_SHARD_VARIANT = 'shard_variant';
17
    const TOKEN_VARIANT = 'variant';
18
    const TOKEN_VERSION = 'version';
19
    const TOKEN_SHARD_FILENAME = 'shard_filename';
20
    const SHARD_SLICE_LENGTH = 3;
21
22
    protected $uuid;
23
    protected $namespace = '';
24
    protected $name = '';
25
    protected $nameAlternative = '';
26
    protected $type = self::TYPE;
27
    protected $variant = self::DEFAULT_VARIANT;
28
    protected $version = self::DEFAULT_VERSION;
29
    protected $author = '';
30
31
    /**
32
     * true if resource file is just created (or should be)
33
     * @var bool
34
     */
35
    protected $new = false;
36
37
    /**
38
     * true if exists resource needs to be recreated
39
     * @var bool
40
     */
41
    protected $recreate = false;
42
43
    /**
44
     * Path to base directory (without dynamic path part)
45
     * @var string
46
     */
47
    protected $baseDirectory = self::DEFAULT_BASE_DIRECTORY;
48
    protected $filePath = '';
49
50
    /**
51
     * List of object properties that should not be iterable (denied for the usage in response)
52
     * @var array
53
     */
54
    protected $notIterable = [
55
        'itemPosition',
56
        'notIterable',
57
        'baseDirectory',
58
        'filePath',
59
        'author',
60
    ];
61
62
    protected $itemPosition = 0;
63
64 75
    public function reset()
65
    {
66 75
        $this->uuid = '';
67 75
        $this->name = '';
68 75
        $this->nameAlternative = '';
69 75
        $this->namespace = '';
70 75
        $this->type = static::TYPE;
71 75
        $this->variant = self::DEFAULT_VARIANT;
72 75
        $this->version = self::DEFAULT_VERSION;
73 75
        $this->author = '';
74 75
        $this->baseDirectory = self::DEFAULT_BASE_DIRECTORY;
75 75
        $this->filePath = '';
76 75
        $this->new = false;
77 75
        $this->recreate = false;
78
79 75
        return $this;
80
    }
81
    abstract public function getMimeType();
82
83 75
    public function __construct()
84
    {
85 75
        $this->reset();
86 75
    }
87 63
    protected function setUuid()
88
    {
89 63
        $this->uuid = md5($this->name . $this->nameAlternative);
90 63
    }
91
92 47
    protected function setFilePath()
93
    {
94 47
        $this->filePath = $this->generateFilePath();
95 47
    }
96
97
    /**
98
     * /type/variant/version/[other-type-specified/]uuid.type
99
     * /mp3/default/1/22af64.mp3
100
     * /mp3/ivona/0/22af64.mp3
101
     */
102 57
    public function generateFilePath()
103
    {
104 57
        $path = '';
105 57
        foreach ($this->getDirectoryTokens() as $token => $slice) {
106 57
            $path .= $slice;
107 57
        }
108 57
        $path .= $this->getUuid() . '.' . $this->getType();
109
110 57
        return $path;
111
    }
112
113
    /**
114
     * Map of the resource directory elements.
115
     * For example, you can use keys with the strtok() method. Or for routes buildings.
116
     *
117
     * @return array
118
     * @see strtok()
119
     * @example strtok($relative_path, '/');
120
     */
121 58
    public function getDirectoryTokens()
122
    {
123
        return [
124 58
            self::TOKEN_BASEDIRECTORY => $this->getBaseDirectory(),
125 58
            self::TOKEN_NAMESPACE => ($this->getNamespace() ? $this->getNamespace() . DIRECTORY_SEPARATOR : ''),
126 58
            self::TOKEN_TYPE => $this->getType() . DIRECTORY_SEPARATOR,
127 58
            self::TOKEN_SHARD_VARIANT => substr($this->getVariant(), 0, self::SHARD_SLICE_LENGTH) . DIRECTORY_SEPARATOR, // Sharding
128 58
            self::TOKEN_VARIANT => $this->getVariant() . DIRECTORY_SEPARATOR,
129 58
            self::TOKEN_VERSION => $this->getVersion() . DIRECTORY_SEPARATOR,
130 58
            self::TOKEN_SHARD_FILENAME => substr($this->getUuid(), 0, self::SHARD_SLICE_LENGTH) . DIRECTORY_SEPARATOR, // Sharding
131 58
        ];
132
    }
133
134
    /**
135
     * Note: Uuid is not really unique, if you want full unique identifier, use hash sum from the full path, for example
136
     * @return mixed
137
     */
138 63
    public function getUuid()
139
    {
140 63
        if (!$this->uuid) {
141 56
            $this->setUuid();
142 56
        }
143 63
        return $this->uuid;
144
    }
145
146
    /**
147
     * @return string
148
     */
149 42
    public function getName()
150
    {
151 42
        return $this->name;
152
    }
153
154
    /**
155
     * @param string $name
156
     * @return ResourceDOInterface
157
     */
158 43
    public function setName($name)
159
    {
160 43
        $this->name = (string)$name;
161 43
        $this->setUuid();
162 43
        $this->setFilePath();
163
164 43
        return $this;
165
    }
166
167
    /**
168
     * @return string
169
     */
170 59
    public function getNamespace()
171
    {
172 59
        return $this->namespace;
173
    }
174
175
    /**
176
     * @param string $namespace
177
     * @return ResourceDOInterface
178
     */
179 16
    public function setNamespace($namespace = '')
180
    {
181 16
        $this->namespace = (string)$namespace;
182 16
        $this->setUuid();
183 16
        $this->setFilePath();
184
185 16
        return $this;
186
    }
187
188
    /**
189
     * @return string
190
     */
191 1
    public function getNameAlternative()
192
    {
193 1
        return $this->nameAlternative;
194
    }
195
196
    /**
197
     * @param string $nameAlternative
198
     * @return ResourceDOInterface
199
     */
200 18
    public function setNameAlternative($nameAlternative = '')
201
    {
202 18
        $this->nameAlternative = (string)$nameAlternative;
203 18
        $this->setUuid();
204 18
        $this->setFilePath();
205
206 18
        return $this;
207
    }
208
209
    /**
210
     * @return string
211
     */
212 64
    public function getType()
213
    {
214 64
        return $this->type;
215
    }
216
    /**
217
     * @param string $type
218
     * @return ResourceDOInterface
219
     */
220 40
    public function setType($type)
221
    {
222 40
        $this->type = (string)$type;
223 40
        $this->setFilePath();
224
225 40
        return $this;
226
    }
227
228
    /**
229
     * @return string
230
     */
231 59
    public function getVariant()
232
    {
233 59
        if (empty($this->variant)) {
234
            $this->setVariant();
235
        }
236
237 59
        return $this->variant;
238
    }
239
240
    /**
241
     * @param string $variant
242
     * @return ResourceDOInterface
243
     */
244 15
    public function setVariant($variant = self::DEFAULT_VARIANT)
245
    {
246 15
        $this->variant = (string)$variant;
247 15
        $this->setFilePath();
248
249 15
        return $this;
250
    }
251
252
    /**
253
     * @return int
254
     */
255 60
    public function getVersion()
256
    {
257 60
        if (self::DEFAULT_VERSION !== $this->version && empty($this->version)) {
258
            $this->setVersion();
259
        }
260 60
        return $this->version;
261
    }
262
263
    /**
264
     * @param int $version
265
     * @return ResourceDOInterface
266
     */
267 31
    public function setVersion($version = self::DEFAULT_VERSION)
268
    {
269 31
        $this->version = (int)$version;
270 31
        $this->setFilePath();
271
272 31
        return $this;
273
    }
274
275
    /**
276
     * @return string
277
     */
278 1
    public function getAuthor()
279
    {
280 1
        return $this->author;
281
    }
282
283
    /**
284
     * @param string $author
285
     * @return ResourceDOInterface
286
     */
287 9
    public function setAuthor($author)
288
    {
289 9
        $this->author = (string)$author;
290
291 9
        return $this;
292
    }
293
294
    /**
295
     * @return string
296
     */
297 45
    public function getFilePath()
298
    {
299 45
        return $this->filePath;
300
    }
301
302
    /**
303
     * @return mixed
304
     */
305 59
    public function getBaseDirectory()
306
    {
307 59
        return $this->baseDirectory;
308
    }
309
310
    /**
311
     * @param string $dir
312
     * @return ResourceDOInterface
313
     */
314 42
    public function setBaseDirectory($dir = self::DEFAULT_BASE_DIRECTORY)
315
    {
316 42
        $dir = (string)$dir;
317 42
        $this->baseDirectory = '' !== $dir
318 42
            ? str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $dir . DIRECTORY_SEPARATOR)
319
320
            // Avoid set basedir to the root directory '/' if base directory is ommited
321 42
            : $dir;
322 42
        $this->setFilePath();
323
324 42
        return $this;
325
    }
326
327
    /**
328
     * @return boolean
329
     */
330 1
    public function isNew()
331
    {
332 1
        return $this->new;
333
    }
334
335
    /**
336
     * @param boolean $new
337
     * @return ResourceDOAbstract
338
     */
339 5
    public function setNew($new = false)
340
    {
341 5
        $this->new = $new;
342
343 5
        return $this;
344
    }
345
346
    /**
347
     * @return boolean
348
     */
349 1
    public function isRecreate()
350
    {
351 1
        return $this->recreate;
352
    }
353
354
    /**
355
     * @param boolean $recreate
356
     * @return ResourceDOAbstract
357
     */
358 5
    public function setRecreate($recreate = false)
359
    {
360 5
        $this->recreate = (bool)$recreate;
361
362 5
        return $this;
363
    }
364
365 4
    public function rewind()
366
    {
367 4
        $this->itemPosition = 0;
368 4
    }
369
370 4
    public function current()
371
    {
372 4
        $props = get_object_vars($this);
373 4
        $propsNames = array_keys($props);
374 4
        sort($propsNames);
375 4
        $propName = $propsNames[$this->itemPosition];
376
377 4
        if (!in_array($propName, $this->notIterable)) {
378
379 4
            return [$propName, $props[$propName]];
380
        }
381
382 4
        return [0, null];
383
    }
384
385 4
    public function key()
386
    {
387 4
        return $this->itemPosition;
388
    }
389
390 4
    public function next()
391
    {
392 4
        ++$this->itemPosition;
393 4
    }
394
395 4
    public function valid()
396
    {
397 4
        $props = get_object_vars($this);
398 4
        $propsNames = array_keys($props);
399 4
        sort($propsNames);
400
401 4
        return isset($propsNames[$this->itemPosition]);
402
    }
403
404 4
    public function toArray()
405
    {
406 4
        $ar = [];
407 4
        foreach ($this as $k => $p) {
408
            
409 4
            $ar[$p[0]] = $p[1];
410 4
        }
411 4
        unset($ar[0]);
412
413 4
        return $ar;
414
    }
415
416
    /**
417
     * Unique resource identifier for ACL
418
     * @return mixed
419
     * @see \Zend\Permissions\Acl\Resource\ResourceInterface::getResourceId
420
     * @see getFilePath
421
     */
422
    public function getResourceId()
423
    {
424
        return $this->getFilePath();
425
    }
426
}