Completed
Push — feature/fixing_cost ( 2c76a0...f58af6 )
by Laurent
01:38
created

AbstractDomainRepository::escape()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
4
namespace FlightLog\Infrastructure\Common\Repository;
5
6
7
abstract class AbstractDomainRepository
8
{
9
    /**
10
     * @var \DoliDB
11
     */
12
    private $db;
13
14
    /**
15
     * @var string
16
     */
17
    private $tableName;
18
19
    /**
20
     * @param \DoliDB $db
21
     * @param string $tableName
22
     */
23
    public function __construct(\DoliDB $db, $tableName)
24
    {
25
        $this->db = $db;
26
        $this->tableName = strpos($tableName, MAIN_DB_PREFIX) === 0 ? $tableName : MAIN_DB_PREFIX.$tableName;
27
    }
28
29
    /**
30
     * @param array $elements
31
     *
32
     * @return float|int
33
     *
34
     * @throws \Exception
35
     */
36
    protected function write(array $elements){
37
        $columns = join(',', array_keys($elements));
38
        $values = join(',', array_map([$this, 'escape'], array_values($elements)));
39
40
        $this->db->begin();
41
        $resql = $this->db->query(sprintf('INSERT INTO ' . $this->tableName . '(%s) VALUES (%s)', $columns, $values));
42
43 View Code Duplication
        if (!$resql) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
            $lasterror = $this->db->lasterror();
45
            dol_syslog(__METHOD__ . ' ' . $lasterror, LOG_ERR);
46
            $this->db->rollback();
47
            throw new \Exception("Repository error - ".$lasterror);
48
        }
49
50
        $id = $this->db->last_insert_id($this->tableName);
51
        $this->db->commit();
52
        return $id;
53
    }
54
55
    /**
56
     * @param mixed $value
57
     *
58
     * @return int|string|null
59
     */
60
    private function escape($value){
61
        if(is_null($value)){
62
            return null;
63
        }
64
65
        if(is_bool($value)){
66
            return (int)$value;
67
        }
68
69
        return is_string($value) ? $this->db->escape($value) : $value ;
70
    }
71
72
    /**
73
     * @param int $id
74
     * @param array $elements
75
     *
76
     * @throws \Exception
77
     */
78
    protected function update($id, array $elements)
79
    {
80
        $sqlModifications = [];
81
82
        foreach($elements as $field => $value){
83
            $sqlModifications[] = sprintf('%s=%s', $field, $this->escape($value));
84
        }
85
86
        $this->db->begin();
87
        $resql = $this->db->query(sprintf('UPDATE %s SET %s WHERE rowid = %s ', $this->tableName, join(',',$sqlModifications), $id));
88
89 View Code Duplication
        if (!$resql) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            $lasterror = $this->db->lasterror();
91
            dol_syslog(__METHOD__ . ' ' . $lasterror, LOG_ERR);
92
            $this->db->rollback();
93
            throw new \Exception("Repository error - ".$lasterror);
94
        }
95
96
        $this->db->commit();
97
    }
98
99
    /**
100
     * Get the entity by its id.
101
     *
102
     * @param int $id
103
     *
104
     * @return array|null
105
     */
106
    protected function get($id){
107
        $sql = sprintf('SELECT * FROM %s WHERE rowid = %s', $this->tableName, $id );
108
109
        $resql = $this->db->query($sql);
110
        if (!$resql) {
111
            return null;
112
        }
113
114
        $num = $this->db->num_rows($resql);
115
        if ($num === 0) {
116
            return null;
117
        }
118
119
        for($i = 0; $i < $num ; $i++) {
0 ignored issues
show
Unused Code introduced by
$i++; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
120
            return $this->db->fetch_array($resql);
121
        }
122
123
        return null;
124
    }
125
126
}