Passed
Push — master ( ca67cf...3841d4 )
by Eugene
11:03
created

ResourceDOAbstract::getBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
440 4
        foreach ($this as $k => $p) {
441
            
442 4
            $ar[$p[0]] = $p[1];
443 4
        }
444 4
        unset($ar[0]);
445
446 4
        return $ar;
447
    }
448
449
    /**
450
     * Unique resource identifier for ACL
451
     * @return mixed
452
     * @see \Zend\Permissions\Acl\Resource\ResourceInterface::getResourceId
453
     * @see getFilePath
454
     */
455
    public function getResourceId()
456
    {
457
        return $this->getFilePath();
458
    }
459
}