SoftDelete::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 16
rs 9.9332
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Action;
5
6
use Sirius\Orm\Entity\StateEnum;
7
8
class SoftDelete extends Delete
9
{
10
    /**
11
     * @var int
12
     */
13
    protected $now;
14
15
    protected function execute()
16
    {
17
        $entityId = $this->mapper->getEntityPk($this->entity);
18
        if (! $entityId) {
19
            return;
20
        }
21
22
        $this->now = time();
23
24
        $update = new \Sirius\Sql\Update($this->mapper->getWriteConnection());
25
        $update->table($this->mapper->getTable())
26
               ->columns([
27
                   $this->getOption('deleted_at_column') => $this->now
28
               ])
29
               ->where('id', $entityId);
30
        $update->perform();
31
    }
32
33
    public function onSuccess()
34
    {
35
        $this->mapper->setEntityAttribute($this->entity, $this->getOption('deleted_at_column'), $this->now);
36
        if ($this->entity->getPersistenceState() !== StateEnum::DELETED) {
37
            $this->entity->setPersistenceState(StateEnum::DELETED);
38
        }
39
    }
40
}
41