Insert::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.8333
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Action;
5
6
use Sirius\Orm\Helpers\Arr;
7
8
class Insert extends Update
9
{
10
    protected $entityId;
11
    protected $entityState;
12
13
    protected $extraColumns = [];
14
15
    protected function execute()
16
    {
17
        $this->entityId    = $this->mapper->getEntityPk($this->entity);
18
        $this->entityState = $this->entity->getPersistenceState();
19
20
        $connection = $this->mapper->getWriteConnection();
21
22
        $columns = array_merge(
23
            $this->mapper->extractFromEntity($this->entity),
24
            $this->extraColumns,
25
            $this->mapper->getGuards()
26
        );
27
        $columns = Arr::except($columns, $this->mapper->getPrimaryKey());
28
29
        $insertSql = new \Sirius\Sql\Insert($connection);
30
        $insertSql->into($this->mapper->getTable())
31
                  ->columns($columns);
32
        $insertSql->perform();
33
        $this->mapper->setEntityPk($this->entity, $connection->lastInsertId());
34
    }
35
36
    public function revert()
37
    {
38
        if (! $this->hasRun) {
39
            return;
40
        }
41
        $this->mapper->setEntityPk($this->entity, $this->entityId);
42
        $this->entity->setPersistenceState($this->entityState);
43
    }
44
}
45