Completed
Push — master ( f144d1...8b1b0d )
by Anton
19s queued 12s
created

src/Command/Database/Delete.php (2 issues)

Labels
1
<?php
2
3
/**
4
 * Cycle DataMapper ORM
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Command\Database;
13
14
use Cycle\ORM\Command\DatabaseCommand;
15
use Cycle\ORM\Command\ScopeCarrierInterface;
16
use Cycle\ORM\Command\Traits\ErrorTrait;
17
use Cycle\ORM\Command\Traits\ScopeTrait;
18
use Cycle\ORM\Exception\CommandException;
19
use Spiral\Database\DatabaseInterface;
20
21
final class Delete extends DatabaseCommand implements ScopeCarrierInterface
22
{
23
    use ScopeTrait;
24
    use ErrorTrait;
0 ignored issues
show
The trait Cycle\ORM\Command\Traits\ErrorTrait requires the property $waitContext which is not provided by Cycle\ORM\Command\Database\Delete.
Loading history...
25
26
    /**
27
     * @param DatabaseInterface $db
28
     * @param string            $table
29
     * @param array             $where
30
     */
31
    public function __construct(DatabaseInterface $db, string $table, array $where = [])
32
    {
33
        parent::__construct($db, $table);
34
        $this->scope = $where;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function isReady(): bool
41
    {
42
        return $this->waitScope === [];
43
    }
44
45
    /**
46
     * Inserting data into associated table.
47
     */
48
    public function execute(): void
49
    {
50
        if ($this->scope === []) {
51
            throw new CommandException('Unable to execute delete command without a scope');
52
        }
53
54
        $this->db->delete($this->table, $this->scope)->run();
0 ignored issues
show
It seems like $this->table can also be of type null; however, parameter $table of Spiral\Database\DatabaseInterface::delete() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $this->db->delete(/** @scrutinizer ignore-type */ $this->table, $this->scope)->run();
Loading history...
55
        parent::execute();
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function register(
62
        string $key,
63
        $value,
64
        bool $fresh = false,
65
        int $stream = self::DATA
66
    ): void {
67
        if ($fresh || $value !== null) {
68
            $this->freeScope($key);
69
        }
70
71
        $this->setScope($key, $value);
72
    }
73
}
74