SoftDelete::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Entity\Behavior;
6
7
use Cycle\ORM\Entity\Behavior\Schema\BaseModifier;
8
use Cycle\ORM\Entity\Behavior\Schema\RegistryModifier;
9
use Cycle\ORM\Entity\Behavior\Listener\SoftDelete as Listener;
10
use Cycle\ORM\Schema\GeneratedField;
11
use Cycle\Schema\Registry;
12
use Doctrine\Common\Annotations\Annotation\Attribute;
13
use Doctrine\Common\Annotations\Annotation\Attributes;
14
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
15
use Doctrine\Common\Annotations\Annotation\Target;
16
17
/**
18
 * SoftDelete implements the soft delete strategy, replaces Delete command with Update command and set current
19
 * timestamp in the configured field.
20
 * Keep in mind that SoftDelete behavior doesn't run events related to Update command.
21
 * The behavior has two parameters:
22
 *    - field - is a property in the entity
23
 *    - column - is a column in the database.
24
 * Behavior requires a field with the DateTime type.
25
 * A property in an entity and a field in the database can be added in several ways:
26
 *   - Can be added by a behavior automatically.
27
 *   - Can be configured with an existing field of the required type in the entity.
28
 *     If the existing field is not of the correct type, or if the property is set for a field in the database that is
29
 *     different from the one specified in the behavior parameters, an exception will be thrown.
30
 *
31
 * @Annotation
32
 * @NamedArgumentConstructor()
33
 * @Target({"CLASS"})
34
 * @Attributes({
35
 *     @Attribute("field", type="string"),
36
 *     @Attribute("column", type="string")
37
 * })
38
 */
39
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
40
final class SoftDelete extends BaseModifier
41
{
42
    private ?string $column = null;
43 16
44
    public function __construct(
45
        private string $field = 'deletedAt',
46
        ?string $column = null,
47 16
    ) {
48
        $this->column = $column;
49
    }
50 16
51
    public function compute(Registry $registry): void
52 16
    {
53
        $modifier = new RegistryModifier($registry, $this->role);
54
        $this->column = $modifier->findColumnName($this->field, $this->column);
55 16
56
        if ($this->column !== null) {
57
            $modifier->addDatetimeColumn($this->column, $this->field)
58 16
                ->nullable(true);
59
        }
60
    }
61
62 16
    public function render(Registry $registry): void
63
    {
64 16
        $modifier = new RegistryModifier($registry, $this->role);
65 16
66
        $this->column = $modifier->findColumnName($this->field, $this->column) ?? $this->field;
67 16
68 16
        $modifier->addDatetimeColumn($this->column, $this->field, GeneratedField::BEFORE_UPDATE)
69
            ->nullable(true);
70
    }
71
72
    protected function getListenerClass(): string
73 16
    {
74
        return Listener::class;
75 16
    }
76
77 16
    protected function getListenerArgs(): array
78
    {
79 16
        return [
80
            'field' => $this->field,
81
        ];
82
    }
83
}
84