Completed
Branch feature/pre-split (c69968)
by Anton
21:25
created

ContextualDeleteCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Commands;
8
9
use Spiral\Database\Entities\Table;
10
use Spiral\ORM\Commands\Traits\ContextTrait;
11
use Spiral\ORM\Commands\Traits\PrimaryTrait;
12
use Spiral\ORM\Commands\Traits\WhereTrait;
13
use Spiral\ORM\ContextualCommandInterface;
14
15
/**
16
 * Contextual delete is command which delete where statement directly linked to it's context
17
 * (mutable delete).
18
 *
19
 * This creates ability to create postponed delete command which where statement will be resolved
20
 * only later in transactions.
21
 */
22
class ContextualDeleteCommand extends TableCommand implements ContextualCommandInterface
23
{
24
    use ContextTrait, PrimaryTrait, WhereTrait;
25
26
    /**
27
     * Where conditions (short where format).
28
     *
29
     * @var array
30
     */
31
    private $where = [];
32
33
    /**
34
     * UpdateCommand constructor.
35
     *
36
     * @param Table $table
37
     * @param array $where
38
     * @param array $values
39
     */
40
    public function __construct(Table $table, array $where, array $values = [])
0 ignored issues
show
Unused Code introduced by
The parameter $values is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        parent::__construct($table);
43
        $this->where = $where;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getDriver()
50
    {
51
        if ($this->isEmpty()) {
52
            //Nothing to do
53
            return null;
54
        }
55
56
        return $this->table->getDatabase()->getDriver();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function isEmpty(): bool
63
    {
64
        return empty($this->where) && empty($this->context);
65
    }
66
67
    /**
68
     * Inserting data into associated table.
69
     */
70
    public function execute()
71
    {
72
        if (!$this->isEmpty()) {
73
            $this->table->delete($this->context + $this->where)->run();
74
        }
75
76
        parent::execute();
77
    }
78
}