AbstractEntity   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 58
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A setId() 0 8 3
A isValidString() 0 8 2
A isVersionValid() 0 8 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: davis
5
 * Date: 7/30/17
6
 * Time: 1:27 PM
7
 */
8
9
namespace DavisPeixoto\BlogCore\Entity;
10
11
use Ramsey\Uuid\Exception\InvalidUuidStringException;
12
use Ramsey\Uuid\Uuid;
13
use Ramsey\Uuid\UuidInterface;
14
use stdClass;
15
16
abstract class AbstractEntity extends stdClass
17
{
18
    const UUID_TYPE_RANDOM = 4;
19
20
    /**
21
     * @var UuidInterface
22
     */
23
    protected $id;
24
25
    /**
26
     * @return string
27
     */
28 9
    public function getId(): string
29
    {
30 9
        return $this->id->toString();
31
    }
32
33
    /**
34
     * @param string|null $id
35
     * @throws InvalidUuidStringException
36
     */
37 26
    public function setId(string $id = null)
38
    {
39 26
        if ($id === null || $id === '') {
40 23
            $this->id = Uuid::uuid4();
41
        } else {
42 13
            $this->id = $this->isVersionValid(Uuid::fromString($this->isValidString($id)));
43
        }
44 24
    }
45
46
    /**
47
     * @param string $id
48
     * @return string
49
     * @throws InvalidUuidStringException
50
     */
51 13
    private function isValidString(string $id): string
52
    {
53 13
        if (!Uuid::isValid($id)) {
54 1
            throw new InvalidUuidStringException('Invalid string');
55
        }
56
57 12
        return $id;
58
    }
59
60
    /**
61
     * @param UuidInterface $uuid
62
     * @return UuidInterface
63
     * @throws InvalidUuidStringException
64
     */
65 12
    private function isVersionValid(UuidInterface $uuid): UuidInterface
66
    {
67 12
        if ($uuid->getVersion() !== self::UUID_TYPE_RANDOM) {
68 1
            throw new InvalidUuidStringException('Not supported version: '.$uuid->getVersion());
69
        }
70
71 11
        return $uuid;
72
    }
73
}
74