Passed
Push — master ( 4ae095...46ef9e )
by Alexander
02:10 queued 13s
created

TimestampedMapper::queueCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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