ContextualDeleteCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
/**
3
 * Spiral, Core Components
4
 *
5
 * @author Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Commands;
9
10
use Spiral\Database\Entities\Table;
11
use Spiral\ORM\Commands\Traits\ContextTrait;
12
use Spiral\ORM\Commands\Traits\PrimaryTrait;
13
use Spiral\ORM\Commands\Traits\WhereTrait;
14
use Spiral\ORM\ContextualCommandInterface;
15
16
/**
17
 * Contextual delete is command which delete where statement directly linked to it's context
18
 * (mutable delete).
19
 *
20
 * This creates ability to create postponed delete command which where statement will be resolved
21
 * only later in transactions.
22
 */
23
class ContextualDeleteCommand extends TableCommand implements ContextualCommandInterface
24
{
25
    use ContextTrait, PrimaryTrait, WhereTrait;
26
27
    /**
28
     * Where conditions (short where format).
29
     *
30
     * @var array
31
     */
32
    private $where = [];
33
34
    /**
35
     * UpdateCommand constructor.
36
     *
37
     * @param Table $table
38
     * @param array $where
39
     * @param array $values
40
     */
41
    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...
42
    {
43
        parent::__construct($table);
44
        $this->where = $where;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getDriver()
51
    {
52
        if ($this->isEmpty()) {
53
            //Nothing to do
54
            return null;
55
        }
56
57
        return $this->table->getDatabase()->getDriver();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function isEmpty(): bool
64
    {
65
        return empty($this->where) && empty($this->context);
66
    }
67
68
    /**
69
     * Inserting data into associated table.
70
     */
71
    public function execute()
72
    {
73
        if (!$this->isEmpty()) {
74
            $this->table->delete($this->context + $this->where)->run();
75
        }
76
77
        parent::execute();
78
    }
79
}