InsertCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 71
rs 10
c 0
b 0
f 0

6 Methods

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