Completed
Push — master ( b1dd66...3d1870 )
by Beñat
04:18 queued 01:05
created

Id::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-2017 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\SharedKernel\Domain\Model\Identity;
13
14
/**
15
 * @author Beñat Espiña <[email protected]>
16
 */
17
abstract class Id implements BaseId
18
{
19
    protected $id;
20
21
    public static function generate($id = null)
22
    {
23
        return new static($id);
24
    }
25
26
    protected function __construct($id = null)
27
    {
28
        if ($id !== null && !is_scalar($id)) {
29
            throw new InvalidIdException();
30
        }
31
        $this->id = null === $id ? Uuid::generate() : $id;
32
    }
33
34
    public function id()
35
    {
36
        return $this->id;
37
    }
38
39
    public function equals(Id $id)
40
    {
41
        return $this->id === $id->id();
42
    }
43
44
    public function __toString()
45
    {
46
        return (string)$this->id;
47
    }
48
}
49