Passed
Branch refatoracao (7827b3)
by William
02:24
created

Create::mountQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Willry\QueryBuilder;
4
5
use Exception;
6
use PDOStatement;
7
use stdClass;
8
9
class Create extends Base
10
{
11
    protected function mountQuery(): void
12
    {
13
        $columns = implode(", ", array_keys($this->fields));
14
        $values = implode(',', array_fill(0, count($this->fields), '?'));
15
16
        $this->query = "INSERT INTO {$this->entity} ({$columns}) VALUES ({$values})";
17
    }
18
19
    /**
20
     * @param array $data
21
     * @return Create
22
     */
23
    public function create(array $data): static
24
    {
25
        $this->setBindings(array_values($data), 'create');
26
        
27
        $this->fields = $data;
28
29
        return $this;
30
    }
31
32
    /**
33
     * @param array $data
34
     */
35
    public function exec(): ?int
36
    {
37
        try {
38
            $this->mountQuery();
39
40
            $stmt = $this->db->prepare($this->query);
41
42
            QueryHelpers::bind($stmt, $this->flatBindings());
43
44
            $stmt->execute();
45
46
            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...
47
        } catch (\PDOException $exception) {
48
            $this->handleError($exception);
49
        }
50
    }
51
}
52