CreatedAt::compute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 3
cts 3
cp 1
crap 2
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\Database\Schema\AbstractColumn;
0 ignored issues
show
Bug introduced by
The type Cycle\Database\Schema\AbstractColumn was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Cycle\ORM\Entity\Behavior\Schema\BaseModifier;
9
use Cycle\ORM\Entity\Behavior\Schema\RegistryModifier;
10
use Cycle\ORM\Entity\Behavior\Listener\CreatedAt as Listener;
11
use Cycle\ORM\Schema\GeneratedField;
12
use Cycle\Schema\Registry;
13
use Doctrine\Common\Annotations\Annotation\Attribute;
14
use Doctrine\Common\Annotations\Annotation\Attributes;
15
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
16
use Doctrine\Common\Annotations\Annotation\Target;
17
18
/**
19
 * CreateadAt behavior will automate adding a creating date to your entity. The behavior has two parameters:
20
 *    - field - is a property in the entity
21
 *    - column - is a column in the database.
22
 * Behavior requires a field with the DateTime type.
23
 * A property in an entity and a field in the database can be added in several ways:
24
 *   - Can be added by a behavior automatically.
25
 *   - Can be configured with an existing field of the required type in the entity.
26
 *     If the existing field is not of the correct type, or if the property is set for a field in the database that is
27
 *     different from the one specified in the behavior parameters, an exception will be thrown.
28
 *
29
 * @Annotation
30
 * @NamedArgumentConstructor()
31
 * @Target({"CLASS"})
32
 * @Attributes({
33
 *     @Attribute("field", type="string"),
34
 *     @Attribute("column", type="string")
35
 * })
36
 */
37
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
38
final class CreatedAt extends BaseModifier
39
{
40
    private ?string $column = null;
41 24
42
    public function __construct(
43
        private string $field = 'createdAt',
44
        ?string $column = null,
45 24
    ) {
46
        $this->column = $column;
47
    }
48 16
49
    public function compute(Registry $registry): void
50 16
    {
51
        $modifier = new RegistryModifier($registry, $this->role);
52
        $this->column = $modifier->findColumnName($this->field, $this->column);
53 16
54
        if ($this->column !== null) {
55
            $modifier->addDatetimeColumn($this->column, $this->field, GeneratedField::BEFORE_INSERT)
56 16
                ->nullable(false)
57
                ->defaultValue(AbstractColumn::DATETIME_NOW);
58
        }
59
    }
60 24
61
    public function render(Registry $registry): void
62 24
    {
63 24
        $modifier = new RegistryModifier($registry, $this->role);
64
65 24
        $this->column = $modifier->findColumnName($this->field, $this->column) ?? $this->field;
66 24
67
        $modifier->addDatetimeColumn($this->column, $this->field, GeneratedField::BEFORE_INSERT)
68
            ->nullable(false)
69
            ->defaultValue(AbstractColumn::DATETIME_NOW);
70
    }
71
72 16
    protected function getListenerClass(): string
73
    {
74 16
        return Listener::class;
75
    }
76 16
77
    protected function getListenerArgs(): array
78 16
    {
79
        return [
80
            'field' => $this->field,
81
        ];
82
    }
83
}
84