Uuid::render()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Entity\Behavior\Uuid;
6
7
use Cycle\ORM\Entity\Behavior\Schema\BaseModifier;
8
use Cycle\ORM\Entity\Behavior\Schema\RegistryModifier;
9
use Cycle\ORM\Schema\GeneratedField;
10
use Cycle\Schema\Registry;
11
use Ramsey\Uuid\Uuid as RamseyUuid;
12
13
abstract class Uuid extends BaseModifier
14
{
15
    protected ?string $column = null;
16
    protected string $field;
17 16
    protected bool $nullable = false;
18
19 16
    public function compute(Registry $registry): void
20 16
    {
21
        $modifier = new RegistryModifier($registry, $this->role);
22 16
        $this->column = $modifier->findColumnName($this->field, $this->column);
23 16
        if ($this->column !== null) {
24 16
            $modifier->addUuidColumn(
25 16
                $this->column,
26 16
                $this->field,
27
                $this->nullable ? null : GeneratedField::BEFORE_INSERT
28
            )->nullable($this->nullable);
29 16
            $modifier->setTypecast(
30
                $registry->getEntity($this->role)->getFields()->get($this->field),
31 16
                [RamseyUuid::class, 'fromString']
32
            );
33 16
        }
34 16
    }
35
36 16
    public function render(Registry $registry): void
37 16
    {
38 16
        $modifier = new RegistryModifier($registry, $this->role);
39 16
        $this->column = $modifier->findColumnName($this->field, $this->column) ?? $this->field;
40
41 16
        $modifier->addUuidColumn(
42
            $this->column,
43
            $this->field,
44
            $this->nullable ? null : GeneratedField::BEFORE_INSERT
45
        )->nullable($this->nullable);
46
        $modifier->setTypecast(
47
            $registry->getEntity($this->role)->getFields()->get($this->field),
48
            [RamseyUuid::class, 'fromString']
49
        );
50
    }
51
}
52