Insert   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A revert() 0 7 2
A execute() 0 19 1
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