Passed
Push — main ( 87c1e5...f29b76 )
by William
01:42
created

Create   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exec() 0 14 2
A mountQuery() 0 6 1
A create() 0 7 1
1
<?php
2
3
namespace Willry\QueryBuilder;
4
5
6
class Create extends Base
7
{
8
    protected function mountQuery(): void
9
    {
10
        $columns = implode(", ", array_keys($this->fields));
11
        $values = implode(',', array_fill(0, count($this->fields), '?'));
12
13
        $this->query = "INSERT INTO {$this->entity} ({$columns}) VALUES ({$values})";
14
    }
15
16
    /**
17
     * @param array $data
18
     * @return Create
19
     */
20
    public function create(array $data): static
21
    {
22
        $this->setBindings(array_values($data), 'create');
23
24
        $this->fields = $data;
25
26
        return $this;
27
    }
28
29
    /**
30
     * @param array $data
31
     */
32
    public function exec(): ?int
33
    {
34
        try {
35
            $this->mountQuery();
36
37
            $stmt = $this->db->prepare($this->query);
38
39
            QueryHelpers::bind($stmt, $this->flatBindings());
40
41
            $stmt->execute();
42
43
            return $this->db->lastInsertId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->lastInsertId() returns the type string which is incompatible with the type-hinted return integer|null.
Loading history...
44
        } catch (\PDOException $exception) {
45
            $this->handleError($exception);
46
        }
47
    }
48
}
49