Completed
Push — master ( 037c01...3910ea )
by Oscar
01:42
created

Insert   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A run() 0 8 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Queries;
5
6
use SimpleCrud\Table;
7
8
final class Insert extends Query
9
{
10
    protected const ALLOWED_METHODS = [
11
        'setFlag',
12
        'set',
13
    ];
14
15
    public function __construct(Table $table, array $data = [])
16
    {
17
        $this->table = $table;
18
19
        $this->query = $table->getDatabase()
20
            ->insert()
21
            ->into((string) $table);
22
23
        foreach ($data as $fieldName => $value) {
24
            $this->table->{$fieldName}->insert($this->query, $value);
25
        }
26
    }
27
28
    public function run()
29
    {
30
        $this->__invoke();
31
32
        $id = $this->table->getDatabase()->lastInsertId();
33
34
        return $this->table->id->format($id);
35
    }
36
}
37