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

SoftDeletedMapper   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A queueDelete() 0 25 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\CommandInterface;
8
use Cycle\ORM\Command\Database\Update;
9
use Cycle\ORM\Context\ConsumerInterface;
10
use Cycle\ORM\Heap\Node;
11
use Cycle\ORM\Heap\State;
12
use Cycle\ORM\Mapper\Mapper;
13
14
/**
15
 * @Table(
16
 *      columns={"deleted_at": @Column(type="datetime", nullable=true)}
17
 * )
18
 */
19
class SoftDeletedMapper extends Mapper
20
{
21
    public function queueDelete($entity, Node $node, State $state): CommandInterface
22
    {
23
        // identify entity as being "deleted"
24
        $state->setStatus(Node::SCHEDULED_DELETE);
25
        $state->decClaim();
26
27
        $cmd = new Update(
28
            $this->source->getDatabase(),
29
            $this->source->getTable(),
30
            ['deleted_at' => new \DateTimeImmutable()]
31
        );
32
33
        // forward primaryKey value from entity state
34
        // this sequence is only required if the entity is created and deleted
35
        // within one transaction
36
        $cmd->waitScope($this->primaryColumn);
37
        $state->forward(
38
            $this->primaryKey,
39
            $cmd,
40
            $this->primaryColumn,
41
            true,
42
            ConsumerInterface::SCOPE
43
        );
44
45
        return $cmd;
46
    }
47
}
48