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

Update   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exec() 0 14 2
A mountQuery() 0 10 2
A update() 0 8 1
1
<?php
2
3
namespace Willry\QueryBuilder;
4
5
use Exception;
6
use PDOStatement;
7
use stdClass;
8
9
class Update extends Base
10
{
11
    protected function mountQuery(): void
12
    {
13
14
        $dateSet = [];
15
        foreach ($this->fields as $bind => $value) {
16
            $dateSet[] = "{$bind} = ?";
17
        }
18
        $dateSet = implode(", ", $dateSet);
19
20
        $this->query = "UPDATE {$this->entity} {$this->joins} SET {$dateSet} {$this->where}";
21
    }
22
23
    /**
24
     * @param array $data
25
     */
26
    public function update(array $data): static
27
    {
28
        $this->type = self::TYPE_UPDATE;
29
30
        $this->setBindings(array_values($data), 'update');
31
        $this->fields = $data;
32
33
        return $this;
34
    }
35
36
    /**
37
     * @param array $data
38
     */
39
    public function exec(): ?int
40
    {
41
        try {
42
            $this->mountQuery();
43
44
            $stmt = $this->db->prepare($this->query);
45
46
            QueryHelpers::bind($stmt, $this->flatBindings());
47
48
            $stmt->execute();
49
50
            return $stmt->rowCount() ?? 1;
51
        } catch (\PDOException $exception) {
52
            $this->handleError($exception);
53
        }
54
    }
55
}
56