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

IdTrait::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
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