Passed
Push — master ( 3e24bf...d48eaa )
by Daniel
05:16
created

IdTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 22.22%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 34
ccs 2
cts 9
cp 0.2222
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A setId() 0 13 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[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
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Entity\Utility;
15
16
use ApiPlatform\Core\Annotation\ApiProperty;
17
use Doctrine\ORM\Mapping as ORM;
18
use Ramsey\Uuid\Codec\OrderedTimeCodec;
19
use Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator;
20
use Ramsey\Uuid\Uuid;
21
use Ramsey\Uuid\UuidInterface;
22
23
/**
24
 * Reusable trait by application developer so keep annotations as we cannot map with XML.
25
 *
26
 * @author Daniel West <[email protected]>
27
 */
28
trait IdTrait
29
{
30
    /**
31
     * Must allow return `null` for lowest dependencies.
32
     *
33
     * @ORM\Id
34
     * @ORM\Column(type="uuid_binary_ordered_time", unique=true)
35
     * @ORM\GeneratedValue(strategy="CUSTOM")
36
     * @ORM\CustomIdGenerator(class=UuidOrderedTimeGenerator::class)
37
     * @ApiProperty(readable=false)
38
     */
39
    protected ?UuidInterface $id = null;
40
41 3
    public function getId(): ?UuidInterface
42
    {
43 3
        return $this->id;
44
    }
45
46
    /**
47
     * @return static
48
     */
49
    public function setId()
50
    {
51
        $factory = clone Uuid::getFactory();
52
53
        $codec = new OrderedTimeCodec(
54
            $factory->getUuidBuilder()
55
        );
56
57
        $factory->setCodec($codec);
58
59
        $this->id = $factory->uuid1();
60
61
        return $this;
62
    }
63
}
64