Passed
Pull Request — develop (#118)
by Laurent
05:08 queued 02:44
created

AbstractUuid   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 12
dl 0
loc 41
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 3 1
A generate() 0 6 2
A fromString() 0 3 1
A __construct() 0 3 1
A fromUuid() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the  G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Domain\Common\Model;
15
16
use Domain\Protocol\Common\UuidProtocol;
17
use Domain\Protocol\IdProtocol;
18
use Ramsey\Uuid\Uuid;
19
use Ramsey\Uuid\UuidInterface;
20
21
abstract class AbstractUuid implements UuidProtocol
22
{
23
    private UuidInterface $uuid;
24
25
    final public function __construct(UuidInterface $uuid)
26
    {
27
        $this->uuid = $uuid;
28
    }
29
30
    public static function generate(): IdProtocol
31
    {
32
        try {
33
            return new static(Uuid::uuid4());
34
        } catch (\Throwable $exception) {
35
            throw new \RuntimeException('Cannot generate a new uuid.', 0, $exception);
36
        }
37
    }
38
39
    public function toString(): string
40
    {
41
        return $this->uuid->toString();
42
    }
43
44
    /**
45
     * @param UuidInterface $uuid
46
     */
47
    public static function fromUuid(object $uuid): UuidProtocol
48
    {
49
        if (!$uuid instanceof UuidInterface) {
50
            throw new \InvalidArgumentException('UuidInterface type excepted.');
51
        }
52
53
        return new static($uuid);
54
    }
55
56
    /**
57
     * @return static
58
     */
59
    public static function fromString(string $uuid)
60
    {
61
        return new static(Uuid::fromString($uuid));
62
    }
63
}
64