Conditions | 8 |
Paths | 17 |
Total Lines | 170 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
152 | protected function saveItem( \Aimeos\MShop\Customer\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Customer\Item\Iface |
||
153 | { |
||
154 | $item = $this->addGroups( $item ); |
||
155 | |||
156 | if( !$item->isModified() ) { |
||
157 | return $this->object()->saveRefs( $item, $fetch ); |
||
158 | } |
||
159 | |||
160 | $context = $this->context(); |
||
161 | $conn = $context->db( $this->getResourceName() ); |
||
162 | |||
163 | $id = $item->getId(); |
||
164 | $billingAddress = $item->getPaymentAddress(); |
||
165 | $columns = $this->object()->getSaveAttributes(); |
||
166 | |||
167 | if( $id === null ) |
||
168 | { |
||
169 | /** mshop/customer/manager/fosuser/insert |
||
170 | * Inserts a new customer record into the database table |
||
171 | * |
||
172 | * Items with no ID yet (i.e. the ID is NULL) will be created in |
||
173 | * the database and the newly created ID retrieved afterwards |
||
174 | * using the "newid" SQL statement. |
||
175 | * |
||
176 | * The SQL statement must be a string suitable for being used as |
||
177 | * prepared statement. It must include question marks for binding |
||
178 | * the values from the customer item to the statement before they are |
||
179 | * sent to the database server. The number of question marks must |
||
180 | * be the same as the number of columns listed in the INSERT |
||
181 | * statement. The order of the columns must correspond to the |
||
182 | * order in the save() method, so the correct values are |
||
183 | * bound to the columns. |
||
184 | * |
||
185 | * The SQL statement should conform to the ANSI standard to be |
||
186 | * compatible with most relational database systems. This also |
||
187 | * includes using double quotes for table and column names. |
||
188 | * |
||
189 | * @param string SQL statement for inserting records |
||
190 | * @since 2015.01 |
||
191 | * @category Developer |
||
192 | * @see mshop/customer/manager/fosuser/update |
||
193 | * @see mshop/customer/manager/fosuser/newid |
||
194 | * @see mshop/customer/manager/fosuser/delete |
||
195 | * @see mshop/customer/manager/fosuser/search |
||
196 | * @see mshop/customer/manager/fosuser/count |
||
197 | */ |
||
198 | $path = 'mshop/customer/manager/fosuser/insert'; |
||
199 | $sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) ); |
||
|
|||
200 | } |
||
201 | else |
||
202 | { |
||
203 | /** mshop/customer/manager/fosuser/update |
||
204 | * Updates an existing customer record in the database |
||
205 | * |
||
206 | * Items which already have an ID (i.e. the ID is not NULL) will |
||
207 | * be updated in the database. |
||
208 | * |
||
209 | * The SQL statement must be a string suitable for being used as |
||
210 | * prepared statement. It must include question marks for binding |
||
211 | * the values from the customer item to the statement before they are |
||
212 | * sent to the database server. The order of the columns must |
||
213 | * correspond to the order in the save() method, so the |
||
214 | * correct values are bound to the columns. |
||
215 | * |
||
216 | * The SQL statement should conform to the ANSI standard to be |
||
217 | * compatible with most relational database systems. This also |
||
218 | * includes using double quotes for table and column names. |
||
219 | * |
||
220 | * @param string SQL statement for updating records |
||
221 | * @since 2015.01 |
||
222 | * @category Developer |
||
223 | * @see mshop/customer/manager/fosuser/insert |
||
224 | * @see mshop/customer/manager/fosuser/newid |
||
225 | * @see mshop/customer/manager/fosuser/delete |
||
226 | * @see mshop/customer/manager/fosuser/search |
||
227 | * @see mshop/customer/manager/fosuser/count |
||
228 | */ |
||
229 | $path = 'mshop/customer/manager/fosuser/update'; |
||
230 | $sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false ); |
||
231 | } |
||
232 | |||
233 | $idx = 1; |
||
234 | $stmt = $this->getCachedStatement( $conn, $path, $sql ); |
||
235 | |||
236 | foreach( $columns as $name => $entry ) { |
||
237 | $stmt->bind( $idx++, $item->get( $name ), \Aimeos\Base\Criteria\SQL::type( $entry->getType() ) ); |
||
238 | } |
||
239 | |||
240 | $stmt->bind( $idx++, $item->getCode() ); // canonical username |
||
241 | $stmt->bind( $idx++, $item->getCode() ); // username |
||
242 | $stmt->bind( $idx++, $billingAddress->getCompany() ); |
||
243 | $stmt->bind( $idx++, $billingAddress->getVatID() ); |
||
244 | $stmt->bind( $idx++, $billingAddress->getSalutation() ); |
||
245 | $stmt->bind( $idx++, $billingAddress->getTitle() ); |
||
246 | $stmt->bind( $idx++, $billingAddress->getFirstname() ); |
||
247 | $stmt->bind( $idx++, $billingAddress->getLastname() ); |
||
248 | $stmt->bind( $idx++, $billingAddress->getAddress1() ); |
||
249 | $stmt->bind( $idx++, $billingAddress->getAddress2() ); |
||
250 | $stmt->bind( $idx++, $billingAddress->getAddress3() ); |
||
251 | $stmt->bind( $idx++, $billingAddress->getPostal() ); |
||
252 | $stmt->bind( $idx++, $billingAddress->getCity() ); |
||
253 | $stmt->bind( $idx++, $billingAddress->getState() ); |
||
254 | $stmt->bind( $idx++, $billingAddress->getCountryId() ); |
||
255 | $stmt->bind( $idx++, $billingAddress->getLanguageId() ); |
||
256 | $stmt->bind( $idx++, $billingAddress->getTelephone() ); |
||
257 | $stmt->bind( $idx++, $billingAddress->getMobile() ); |
||
258 | $stmt->bind( $idx++, $billingAddress->getEmail() ); |
||
259 | $stmt->bind( $idx++, $billingAddress->getEmail() ); |
||
260 | $stmt->bind( $idx++, $billingAddress->getTelefax() ); |
||
261 | $stmt->bind( $idx++, $billingAddress->getWebsite() ); |
||
262 | $stmt->bind( $idx++, $billingAddress->getLongitude(), \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT ); |
||
263 | $stmt->bind( $idx++, $billingAddress->getLatitude(), \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT ); |
||
264 | $stmt->bind( $idx++, $billingAddress->getBirthday() ); |
||
265 | $stmt->bind( $idx++, ( $item->getStatus() > 0 ? true : false ), \Aimeos\Base\DB\Statement\Base::PARAM_BOOL ); |
||
266 | $stmt->bind( $idx++, $item->getDateVerified() ); |
||
267 | $stmt->bind( $idx++, $item->getPassword() ); |
||
268 | $stmt->bind( $idx++, $context->datetime() ); // Modification time |
||
269 | $stmt->bind( $idx++, $context->editor() ); |
||
270 | $stmt->bind( $idx++, serialize( $item->getRoles() ) ); |
||
271 | $stmt->bind( $idx++, $item->getSalt() ); |
||
272 | |||
273 | if( $id !== null ) { |
||
274 | $stmt->bind( $idx++, $context->locale()->getSiteId() . '%' ); |
||
275 | $stmt->bind( $idx++, (string) $context->user()?->getSiteId() ); |
||
276 | $stmt->bind( $idx, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
||
277 | $billingAddress->setId( $id ); // enforce ID to be present |
||
278 | } else { |
||
279 | $stmt->bind( $idx++, $this->siteId( $item->getSiteId(), \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) ); |
||
280 | $stmt->bind( $idx, $context->datetime() ); // Creation time |
||
281 | } |
||
282 | |||
283 | $stmt->execute()->finish(); |
||
284 | |||
285 | if( $id === null && $fetch === true ) |
||
286 | { |
||
287 | /** mshop/customer/manager/fosuser/newid |
||
288 | * Retrieves the ID generated by the database when inserting a new record |
||
289 | * |
||
290 | * As soon as a new record is inserted into the database table, |
||
291 | * the database server generates a new and unique identifier for |
||
292 | * that record. This ID can be used for retrieving, updating and |
||
293 | * deleting that specific record from the table again. |
||
294 | * |
||
295 | * For MySQL: |
||
296 | * SELECT LAST_INSERT_ID() |
||
297 | * For PostgreSQL: |
||
298 | * SELECT currval('seq_mcus_id') |
||
299 | * For SQL Server: |
||
300 | * SELECT SCOPE_IDENTITY() |
||
301 | * For Oracle: |
||
302 | * SELECT "seq_mcus_id".CURRVAL FROM DUAL |
||
303 | * |
||
304 | * There's no way to retrive the new ID by a SQL statements that |
||
305 | * fits for most database servers as they implement their own |
||
306 | * specific way. |
||
307 | * |
||
308 | * @param string SQL statement for retrieving the last inserted record ID |
||
309 | * @since 2015.01 |
||
310 | * @category Developer |
||
311 | * @see mshop/customer/manager/fosuser/insert |
||
312 | * @see mshop/customer/manager/fosuser/update |
||
313 | * @see mshop/customer/manager/fosuser/delete |
||
314 | * @see mshop/customer/manager/fosuser/search |
||
315 | * @see mshop/customer/manager/fosuser/count |
||
316 | */ |
||
317 | $path = 'mshop/customer/manager/fosuser/newid'; |
||
318 | $id = $this->newId( $conn, $path ); |
||
319 | } |
||
320 | |||
321 | return $this->object()->saveRefs( $item->setId( $id ), $fetch ); |
||
322 | } |
||
364 |