Completed
Branch feature/pre-split (289154)
by Anton
03:22
created

InsertCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
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\ContextualCommandInterface;
12
13
/**
14
 * Inserted data CAN be modified by parent commands using context.
15
 */
16
class InsertCommand extends TableCommand implements ContextualCommandInterface
17
{
18
    use ContextTrait;
19
20
    /**
21
     * @var array
22
     */
23
    private $data = [];
24
25
    /**
26
     * Set when command is executed.
27
     *
28
     * @var null|mixed
29
     */
30
    private $insertID = null;
31
32
    /**
33
     * @param Table $table
34
     * @param array $data
35
     */
36
    public function __construct(Table $table, array $data = [])
37
    {
38
        parent::__construct($table);
39
        $this->data = $data;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function isEmpty(): bool
46
    {
47
        return empty($this->data) && empty($this->context);
48
    }
49
50
    /**
51
     * Insert values, context not included.
52
     *
53
     * @return array
54
     */
55
    public function getData(): array
56
    {
57
        return $this->data;
58
    }
59
60
    /**
61
     * Get inserted row id.
62
     *
63
     * @return mixed|null
64
     */
65
    public function getInsertID()
66
    {
67
        return $this->insertID;
68
    }
69
70
    /**
71
     * @return mixed|null
72
     */
73
    public function primaryKey()
74
    {
75
        return $this->insertID;
76
    }
77
78
    /**
79
     * Inserting data into associated table.
80
     */
81
    public function execute()
82
    {
83
        $this->insertID = $this->table->insertOne($this->context + $this->data);
84
        parent::execute();
85
    }
86
}
87