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

SoftDeletedMapper::queueDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
nc 1
nop 3
dl 0
loc 25
ccs 0
cts 18
cp 0
c 1
b 0
f 0
cc 1
crap 2
rs 9.7998
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