Passed
Pull Request — master (#4)
by
unknown
01:53
created

TimestampedMapper   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 24
ccs 0
cts 15
cp 0
c 1
b 0
f 0
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A queueCreate() 0 11 1
A queueUpdate() 0 9 1
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Mapper;
4
5
use Cycle\Annotated\Annotation\Column;
6
use Cycle\Annotated\Annotation\Table;
7
use Cycle\ORM\Command\ContextCarrierInterface;
8
use Cycle\ORM\Command\Database\Update;
9
use Cycle\ORM\Heap\Node;
10
use Cycle\ORM\Heap\State;
11
use Cycle\ORM\Mapper\Mapper;
12
13
/**
14
 * You can use the annotated entities extension to automatically declare the needed columns from inside your mapper
15
 * @See https://github.com/cycle/docs/blob/master/advanced/timestamp.md#automatically-define-columns
16
 *
17
 * @Table(
18
 *      columns={"created_at": @Column(type="datetime"), "updated_at": @Column(type="datetime")}
19
 * )
20
 */
21
class TimestampedMapper extends Mapper
22
{
23
    public function queueCreate($entity, Node $node, State $state): ContextCarrierInterface
24
    {
25
        $command = parent::queueCreate($entity, $node, $state);
26
27
        $state->register('created_at', new \DateTimeImmutable(), true);
28
        $command->register('created_at', new \DateTimeImmutable(), true);
29
30
        $state->register('updated_at', new \DateTimeImmutable(), true);
31
        $command->register('updated_at', new \DateTimeImmutable(), true);
32
33
        return $command;
34
    }
35
36
    public function queueUpdate($entity, Node $node, State $state): ContextCarrierInterface
37
    {
38
        /** @var Update $command */
39
        $command = parent::queueUpdate($entity, $node, $state);
40
41
        $state->register('updated_at', new \DateTimeImmutable(), true);
42
        $command->registerAppendix('updated_at', new \DateTimeImmutable());
43
44
        return $command;
45
    }
46
}
47