Passed
Push — master ( 4ae095...46ef9e )
by Alexander
02:10 queued 13s
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
 * You can use the annotated entities extension to automatically declare the needed columns from inside your mapper
16
 * @See https://github.com/cycle/docs/blob/master/advanced/soft-deletes.md
17
 *
18
 * @Table(
19
 *      columns={"deleted_at": @Column(type="datetime", nullable=true)}
20
 * )
21
 */
22
class SoftDeletedMapper extends Mapper
23
{
24
    public function queueDelete($entity, Node $node, State $state): CommandInterface
25
    {
26
        // identify entity as being "deleted"
27
        $state->setStatus(Node::SCHEDULED_DELETE);
28
        $state->decClaim();
29
30
        $command = new Update(
31
            $this->source->getDatabase(),
32
            $this->source->getTable(),
33
            ['deleted_at' => new \DateTimeImmutable()]
34
        );
35
36
        // forward primaryKey value from entity state
37
        // this sequence is only required if the entity is created and deleted
38
        // within one transaction
39
        $command->waitScope($this->primaryColumn);
40
        $state->forward(
41
            $this->primaryKey,
42
            $command,
43
            $this->primaryColumn,
44
            true,
45
            ConsumerInterface::SCOPE
46
        );
47
48
        return $command;
49
    }
50
}
51