Completed
Push — master ( 5bff48...0eeb04 )
by Oscar
02:44
created

Insert::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace SimpleCrud\Queries\Mysql;
4
5
use SimpleCrud\Queries\BaseQuery;
6
use SimpleCrud\Entity;
7
use PDOStatement;
8
9
/**
10
 * Manages a database insert query in Mysql databases.
11
 */
12
class Insert extends BaseQuery
13
{
14
    protected $data = [];
15
    protected $duplications;
16
17
    /**
18
     * Set the data to update.
19
     *
20
     * @param array $data
21
     *
22
     * @return self
23
     */
24
    public function data(array $data)
25
    {
26
        $this->data = $this->entity->prepareDataToDatabase($data, true);
27
28
        return $this;
29
    }
30
31
    /**
32
     * Set true to handle duplications.
33
     *
34
     * @param bool $handle
35
     *
36
     * @return self
37
     */
38
    public function duplications($handle = true)
39
    {
40
        $this->duplications = $handle;
41
42
        return $this;
43
    }
44
45
    /**
46
     * Run the query and return the id.
47
     *
48
     * @return int
49
     */
50
    public function run()
51
    {
52
        $this->__invoke();
53
54
        $id = $this->entity->getDb()->lastInsertId();
55
56
        return $this->entity->fields['id']->dataFromDatabase($id);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 View Code Duplication
    public function __invoke()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
63
    {
64
        $marks = [];
65
66
        foreach ($this->data as $field => $value) {
67
            $marks[":{$field}"] = $value;
68
        }
69
70
        return $this->entity->getDb()->execute((string) $this, $marks);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function __toString()
77
    {
78
        if (empty($this->data)) {
79
            return "INSERT INTO `{$this->entity->name}` (`id`) VALUES (NULL)";
80
        }
81
82
        $fields = array_keys($this->data);
83
84
        $query = "INSERT INTO `{$this->entity->name}`";
85
        $query .= ' (`'.implode('`, `', $fields).'`)';
86
        $query .= ' VALUES (:'.implode(', :', $fields).')';
87
88
        if ($this->duplications) {
89
            $query .= ' ON DUPLICATE KEY UPDATE';
90
            $query .= ' id = LAST_INSERT_ID(id), '.static::buildFields($fields);
91
        }
92
93
        return $query;
94
    }
95
96
    /**
97
     * Generates the data part of a UPDATE query.
98
     *
99
     * @param array $fields
100
     *
101
     * @return string
102
     */
103 View Code Duplication
    protected static function buildFields(array $fields)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
104
    {
105
        $query = [];
106
107
        foreach ($fields as $field) {
108
            $query[] = "`{$field}` = :{$field}";
109
        }
110
111
        return implode(', ', $query);
112
    }
113
}
114