ListCollectionUuid   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUuid() 0 3 1
A __toString() 0 3 1
A setUuid() 0 17 4
A __construct() 0 3 1
1
<?php
2
/**
3
 * This file is part of the InMemoryList package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 */
10
11
namespace InMemoryList\Domain\Model;
12
13
use InMemoryList\Domain\Model\Contracts\ListRepositoryInterface;
14
use InMemoryList\Domain\Model\Exceptions\ListCollectionNotAllowedUuidException;
15
use Ramsey\Uuid\Uuid;
16
17
class ListCollectionUuid
18
{
19
    /**
20
     * @var string
21
     */
22
    private $uuid;
23
24
    /**
25
     * ListCollectionUUId constructor.
26
     *
27
     * @param null $uuid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $uuid is correct as it would always require null to be passed?
Loading history...
28
     */
29
    public function __construct($uuid = null)
30
    {
31
        $this->setUuid($uuid);
32
    }
33
34
    /**
35
     * @param null $uuid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $uuid is correct as it would always require null to be passed?
Loading history...
36
     *
37
     * @throws ListCollectionNotAllowedUuidException
38
     */
39
    public function setUuid($uuid = null)
40
    {
41
        $notAllowedNames = [
42
            ListRepositoryInterface::CHUNK,
43
            ListRepositoryInterface::HEADERS,
44
            ListRepositoryInterface::INDEX,
45
            ListRepositoryInterface::SEPARATOR,
46
            ListRepositoryInterface::STATISTICS,
47
        ];
48
49
        foreach ($notAllowedNames as $notAllowedName) {
50
            if (strpos($uuid, $notAllowedName) !== false) {
51
                throw new ListCollectionNotAllowedUuidException('You can\'t assign "'.$uuid.'" as list uuid.');
52
            }
53
        }
54
55
        $this->uuid = str_replace(' ', '-', $uuid) ?: Uuid::uuid4()->toString();
56
    }
57
58
    /**
59
     * @return mixed
60
     */
61
    public function getUuid()
62
    {
63
        return $this->uuid;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function __toString()
70
    {
71
        return $this->getUuid();
72
    }
73
}
74