1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the moss-storage package |
5
|
|
|
* |
6
|
|
|
* (c) Michal Wachowski <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Moss\Storage\Query; |
13
|
|
|
|
14
|
|
|
use Moss\Storage\GetTypeTrait; |
15
|
|
|
use Moss\Storage\Model\Definition\FieldInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Query used to read data from table |
19
|
|
|
* |
20
|
|
|
* @author Michal Wachowski <[email protected]> |
21
|
|
|
* @package Moss\Storage |
22
|
|
|
*/ |
23
|
|
|
class WriteQuery extends AbstractEntityValueQuery implements WriteQueryInterface |
24
|
|
|
{ |
25
|
|
|
const EVENT_BEFORE = 'write.before'; |
26
|
|
|
const EVENT_AFTER = 'write.after'; |
27
|
|
|
|
28
|
|
|
use GetTypeTrait; |
29
|
|
|
|
30
|
|
|
protected $instance; |
31
|
|
|
|
32
|
|
|
protected $values = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets field names which values will be written |
36
|
|
|
* |
37
|
|
|
* @param array $fields |
38
|
|
|
* |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function values($fields = []) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->values = []; |
44
|
|
|
|
45
|
|
|
if (empty($fields)) { |
46
|
|
|
foreach ($this->model->fields() as $field) { |
47
|
|
|
$this->assignValue($field); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($fields as $field) { |
54
|
|
|
$this->assignValue($this->model->field($field)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Adds field which value will be written |
62
|
|
|
* |
63
|
|
|
* @param string $field |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function value($field) |
68
|
|
|
{ |
69
|
|
|
$this->assignValue($this->model->field($field)); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Assigns value to query |
76
|
|
|
* |
77
|
|
|
* @param FieldInterface $field |
78
|
|
|
*/ |
79
|
|
|
protected function assignValue(FieldInterface $field) |
80
|
|
|
{ |
81
|
|
|
$this->values[] = $field->name(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Executes query |
87
|
|
|
* After execution query is reset |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function execute() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$this->dispatcher->fire(self::EVENT_BEFORE, $this->instance); |
94
|
|
|
|
95
|
|
|
$this->buildQuery() |
96
|
|
|
->execute(); |
97
|
|
|
|
98
|
|
|
$this->dispatcher->fire(self::EVENT_AFTER, $this->instance); |
99
|
|
|
|
100
|
|
|
foreach ($this->relations as $relation) { |
101
|
|
|
$relation->write($this->instance); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->instance; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns current query string |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getSQL() |
113
|
|
|
{ |
114
|
|
|
return $this->buildQuery()->getSQL(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return InsertQuery|UpdateQuery |
119
|
|
|
*/ |
120
|
|
|
protected function buildQuery() |
121
|
|
|
{ |
122
|
|
|
if ($this->checkIfEntityExists()) { |
123
|
|
|
$query = new UpdateQuery($this->connection, $this->instance, $this->model, $this->factory, $this->accessor, $this->dispatcher); |
124
|
|
|
} else { |
125
|
|
|
$query = new InsertQuery($this->connection, $this->instance, $this->model, $this->factory, $this->accessor, $this->dispatcher); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$query->values($this->values); |
129
|
|
|
|
130
|
|
|
return $query; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns true if entity exists database |
135
|
|
|
* |
136
|
|
|
* @return int |
137
|
|
|
*/ |
138
|
|
|
protected function checkIfEntityExists() |
139
|
|
|
{ |
140
|
|
|
$query = new ReadQuery($this->connection, $this->model, $this->factory, $this->accessor, $this->dispatcher); |
141
|
|
|
|
142
|
|
|
foreach ($this->model->primaryFields() as $field) { |
143
|
|
|
$value = $this->accessor->getPropertyValue($this->instance, $field->name()); |
144
|
|
|
|
145
|
|
|
if ($value === null) { |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$query->where($field->name(), $value, '=', 'and'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $query->count() > 0; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns array with bound values and their placeholders as keys |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
public function binds() |
161
|
|
|
{ |
162
|
|
|
return []; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Resets adapter |
167
|
|
|
* |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function reset() |
171
|
|
|
{ |
172
|
|
|
$this->values = []; |
173
|
|
|
$this->relations = []; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
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.