Passed
Push — master ( 268116...cc8508 )
by Nícollas
01:20
created

CRUD::insert()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 17
rs 9.8333
1
<?php
2
3
namespace SimplePHP\Model;
4
5
use PDOException;
6
7
/**
8
 * Trait CRUD to SimplePHP
9
 * @package NicollasSilva\SimplePHP
10
 */
11
trait CRUD {
12
13
    /**
14
     * @param int $primary
15
     * @return bool
16
     */
17
    public function delete(int $primary): ?bool
18
    {
19
        try {
20
            $sql = $this->conn->prepare("DELETE FROM {$this->table} WHERE {$this->primary} = :primary");
21
            $sql->bindParam(':primary', $primary);
22
            return $sql->execute();
23
        } catch (PDOException $exception) {
24
            return null;
25
        }
26
    }
27
28
    /**
29
     * @param string $params
30
     * @param array $values
31
     * @param int $primary
32
     * @return PDOException|bool
33
     */
34
    public function update(String $params, Array $values, Int $primary)
35
    {
36
        try {
37
        $params = explode(',', $params);
38
        $data = [];
39
        $countParams = count($params);
40
        for ($i = 0; $i < $countParams; $i++) {
41
            $data[$i] = ":" . $params[$i] . $i . ", ";
42
        }
43
        $result = '';
44
        $final = array_map(null, $params, $data);
45
        foreach ($final as $key => $vals) {
46
            foreach ($vals as $chave => $val) {
47
                $result .= str_replace(':', ' = :', $val);
48
            }
49
        }
50
        $result = rtrim($result, ', ');
51
        $sql = $this->conn->prepare("UPDATE {$this->table} SET {$result} WHERE {$this->primary} = '{$primary}'");
52
        for ($i = 0; $i < $countParams; $i++) {
53
            $data[$i] = ":" . $params[$i] . $i;
54
        }
55
        $countData = count($data);
56
        for ($i = 0; $i < $countData; $i++) {
57
            $sql->bindParam($data[$i], $values[$i]);
58
        }
59
        return $sql->execute();
60
        } catch(PDOException $exception) {
61
            echo $exception->getMessage();
62
        }
63
    }
64
65
    /**
66
     * @param string $params
67
     * @param array $values
68
     * @return PDOException|bool
69
     */
70
    public function insert(String $params, Array $values)
71
    {
72
        try {
73
            $parameters = "(".$params.")";
74
            $params = explode(',', $params);
75
            $data = [];
76
                for($i = 0; $i < count($params); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
77
                    $data[$i] = ":". $params[$i] . $i;
78
                }
79
            $valueBind = "(".implode(', ', $data).")";
80
            $sql = $this->conn->prepare("INSERT INTO {$this->table} $parameters VALUES $valueBind");
81
                for($i = 0; $i < count($params); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
82
                    $sql->bindParam($data[$i], $values[$i]);
83
                }
84
            return $sql->execute();
85
        } catch(PDOException $exception) {
86
            echo $exception->getCode();
87
        }
88
    }
89
}
90