← All changes
|
src/Packetery/Module/CustomsDeclaration/Repository.php
+54
-62
trunk
→
2.1
View file →
| @@ -12,9 +12,8 @@ | ||
| 12 | 12 | use Packetery\Core\CoreHelper; |
| 13 | 13 | use Packetery\Core\Entity\CustomsDeclaration; |
| 14 | 14 | use Packetery\Core\Entity\CustomsDeclarationItem; |
| 15 | 15 | use Packetery\Module\EntityFactory; |
| 16 | -use Packetery\Module\Exception\DeleteErrorException; | |
| 17 | 16 | use Packetery\Module\WpdbAdapter; |
| 18 | 17 | |
| 19 | 18 | /** |
| 20 | 19 | * Class Repository. |
| @@ -178,21 +177,19 @@ | ||
| 178 | 177 | * Saves customs declaration. |
| 179 | 178 | * |
| 180 | 179 | * @param CustomsDeclaration $customsDeclaration Customs declaration. |
| 181 | 180 | * @param array $fieldsToOmit Fields to omit. |
| 182 | - * @return int|false The number of rows updated, or false on error. | |
| 181 | + * @return void | |
| 183 | 182 | */ |
| 184 | - public function save( CustomsDeclaration $customsDeclaration, array $fieldsToOmit = [ 'invoice_file', 'ead_file' ] ) { | |
| 183 | + public function save( CustomsDeclaration $customsDeclaration, array $fieldsToOmit = [ 'invoice_file', 'ead_file' ] ): void { | |
| 185 | 184 | if ( $customsDeclaration->getId() === null ) { |
| 186 | - $updatedRowCount = $this->wpdbAdapter->insertReplaceHelper( | |
| 185 | + $this->wpdbAdapter->insertReplaceHelper( | |
| 187 | 186 | $this->wpdbAdapter->packeteryCustomsDeclaration, |
| 188 | 187 | $this->declarationToDbArray( $customsDeclaration, $fieldsToOmit ) |
| 189 | 188 | ); |
| 190 | - if ( $updatedRowCount !== false ) { | |
| 191 | - $customsDeclaration->setId( $this->wpdbAdapter->getLastInsertId() ); | |
| 192 | - } | |
| 189 | + $customsDeclaration->setId( $this->wpdbAdapter->getLastInsertId() ); | |
| 193 | 190 | } else { |
| 194 | - $updatedRowCount = $this->wpdbAdapter->update( | |
| 191 | + $this->wpdbAdapter->update( | |
| 195 | 192 | $this->wpdbAdapter->packeteryCustomsDeclaration, |
| 196 | 193 | $this->declarationToDbArray( $customsDeclaration, $fieldsToOmit ), |
| 197 | 194 | [ 'id' => (int) $customsDeclaration->getId() ] |
| 198 | 195 | ); |
| @@ -197,57 +194,47 @@ | ||
| 197 | 194 | [ 'id' => (int) $customsDeclaration->getId() ] |
| 198 | 195 | ); |
| 199 | 196 | } |
| 200 | 197 | |
| 201 | - if ( $customsDeclaration->getId() !== null ) { | |
| 202 | - $omitInvoiceFile = in_array( 'invoice_file', $fieldsToOmit, true ); | |
| 203 | - if ( $omitInvoiceFile === false && $customsDeclaration->hasInvoiceFileContent() ) { | |
| 204 | - $fileQueryResult = $this->wpdbAdapter->update( | |
| 205 | - $this->wpdbAdapter->packeteryCustomsDeclaration, | |
| 206 | - [ 'invoice_file' => $customsDeclaration->getInvoiceFile() ], | |
| 207 | - [ 'id' => (int) $customsDeclaration->getId() ] | |
| 208 | - ); | |
| 209 | - if ( $fileQueryResult === false ) { | |
| 210 | - $updatedRowCount = false; | |
| 211 | - } | |
| 212 | - } | |
| 198 | + $omitInvoiceFile = in_array( 'invoice_file', $fieldsToOmit, true ); | |
| 199 | + if ( $omitInvoiceFile === false && $customsDeclaration->hasInvoiceFileContent() ) { | |
| 200 | + $this->wpdbAdapter->query( | |
| 201 | + $this->wpdbAdapter->prepare( | |
| 202 | + 'UPDATE `' . $this->wpdbAdapter->packeteryCustomsDeclaration . '` SET `invoice_file` = %s WHERE `id` = %d', | |
| 203 | + $customsDeclaration->getInvoiceFile(), | |
| 204 | + $customsDeclaration->getId() | |
| 205 | + ) | |
| 206 | + ); | |
| 207 | + } | |
| 213 | 208 | |
| 214 | - if ( $omitInvoiceFile === false && $customsDeclaration->hasInvoiceFileContent() === false ) { | |
| 215 | - $fileQueryResult = $this->wpdbAdapter->update( | |
| 216 | - $this->wpdbAdapter->packeteryCustomsDeclaration, | |
| 217 | - [ 'invoice_file' => null ], | |
| 218 | - [ 'id' => (int) $customsDeclaration->getId() ] | |
| 219 | - ); | |
| 220 | - if ( $fileQueryResult === false ) { | |
| 221 | - $updatedRowCount = false; | |
| 222 | - } | |
| 223 | - } | |
| 209 | + if ( $omitInvoiceFile === false && $customsDeclaration->hasInvoiceFileContent() === false ) { | |
| 210 | + $this->wpdbAdapter->query( | |
| 211 | + $this->wpdbAdapter->prepare( | |
| 212 | + 'UPDATE ' . $this->wpdbAdapter->packeteryCustomsDeclaration . ' SET `invoice_file` = NULL WHERE `id` = %d', | |
| 213 | + $customsDeclaration->getId() | |
| 214 | + ) | |
| 215 | + ); | |
| 216 | + } | |
| 224 | 217 | |
| 225 | - $omitEadFile = in_array( 'ead_file', $fieldsToOmit, true ); | |
| 226 | - if ( $omitEadFile === false && $customsDeclaration->hasEadFileContent() ) { | |
| 227 | - $fileQueryResult = $this->wpdbAdapter->update( | |
| 228 | - $this->wpdbAdapter->packeteryCustomsDeclaration, | |
| 229 | - [ 'ead_file' => $customsDeclaration->getEadFile() ], | |
| 230 | - [ 'id' => (int) $customsDeclaration->getId() ] | |
| 231 | - ); | |
| 232 | - if ( $fileQueryResult === false ) { | |
| 233 | - $updatedRowCount = false; | |
| 234 | - } | |
| 235 | - } | |
| 218 | + $omitEadFile = in_array( 'ead_file', $fieldsToOmit, true ); | |
| 219 | + if ( $omitEadFile === false && $customsDeclaration->hasEadFileContent() ) { | |
| 220 | + $this->wpdbAdapter->query( | |
| 221 | + $this->wpdbAdapter->prepare( | |
| 222 | + 'UPDATE `' . $this->wpdbAdapter->packeteryCustomsDeclaration . '` SET `ead_file` = %s WHERE `id` = %d', | |
| 223 | + $customsDeclaration->getEadFile(), | |
| 224 | + $customsDeclaration->getId() | |
| 225 | + ) | |
| 226 | + ); | |
| 227 | + } | |
| 236 | 228 | |
| 237 | - if ( $omitEadFile === false && $customsDeclaration->hasEadFileContent() === false ) { | |
| 238 | - $fileQueryResult = $this->wpdbAdapter->update( | |
| 239 | - $this->wpdbAdapter->packeteryCustomsDeclaration, | |
| 240 | - [ 'ead_file' => null ], | |
| 241 | - [ 'id' => (int) $customsDeclaration->getId() ] | |
| 242 | - ); | |
| 243 | - if ( $fileQueryResult === false ) { | |
| 244 | - $updatedRowCount = false; | |
| 245 | - } | |
| 246 | - } | |
| 229 | + if ( $omitEadFile === false && $customsDeclaration->hasEadFileContent() === false ) { | |
| 230 | + $this->wpdbAdapter->query( | |
| 231 | + $this->wpdbAdapter->prepare( | |
| 232 | + 'UPDATE ' . $this->wpdbAdapter->packeteryCustomsDeclaration . ' SET `ead_file` = NULL WHERE `id` = %d', | |
| 233 | + $customsDeclaration->getId() | |
| 234 | + ) | |
| 235 | + ); | |
| 247 | 236 | } |
| 248 | - | |
| 249 | - return $updatedRowCount; | |
| 250 | 237 | } |
| 251 | 238 | |
| 252 | 239 | /** |
| 253 | 240 | * Saves customs declaration item. |
| @@ -252,30 +239,31 @@ | ||
| 252 | 239 | /** |
| 253 | 240 | * Saves customs declaration item. |
| 254 | 241 | * |
| 255 | 242 | * @param CustomsDeclarationItem $customsDeclarationItem Customs declaration item. |
| 256 | - * @return int|false The number of rows updated, or false on error. | |
| 243 | + * @return void | |
| 257 | 244 | */ |
| 258 | - public function saveItem( CustomsDeclarationItem $customsDeclarationItem ) { | |
| 245 | + public function saveItem( CustomsDeclarationItem $customsDeclarationItem ): void { | |
| 259 | 246 | if ( $customsDeclarationItem->getId() === null ) { |
| 260 | - $updatedRowCount = $this->wpdbAdapter->insert( | |
| 247 | + $this->wpdbAdapter->insert( | |
| 261 | 248 | $this->wpdbAdapter->packeteryCustomsDeclarationItem, |
| 262 | 249 | $this->declarationItemToDbArray( $customsDeclarationItem ) |
| 263 | 250 | ); |
| 264 | 251 | $customsDeclarationItem->setId( $this->wpdbAdapter->getLastInsertId() ); |
| 265 | 252 | } else { |
| 266 | - $updatedRowCount = $this->wpdbAdapter->update( | |
| 253 | + $this->wpdbAdapter->update( | |
| 267 | 254 | $this->wpdbAdapter->packeteryCustomsDeclarationItem, |
| 268 | 255 | $this->declarationItemToDbArray( $customsDeclarationItem ), |
| 269 | 256 | [ 'id' => (int) $customsDeclarationItem->getId() ] |
| 270 | 257 | ); |
| 271 | 258 | } |
| 272 | - | |
| 273 | - return $updatedRowCount; | |
| 274 | 259 | } |
| 275 | 260 | |
| 276 | 261 | /** |
| 277 | - * @throws DeleteErrorException | |
| 262 | + * Deletes item. | |
| 263 | + * | |
| 264 | + * @param int $itemId Item ID. | |
| 265 | + * @return void | |
| 278 | 266 | */ |
| 279 | 267 | public function deleteItem( int $itemId ): void { |
| 280 | 268 | $this->wpdbAdapter->delete( $this->wpdbAdapter->packeteryCustomsDeclarationItem, [ 'id' => $itemId ], '%d' ); |
| 281 | 269 | } |
| @@ -280,9 +268,12 @@ | ||
| 280 | 268 | $this->wpdbAdapter->delete( $this->wpdbAdapter->packeteryCustomsDeclarationItem, [ 'id' => $itemId ], '%d' ); |
| 281 | 269 | } |
| 282 | 270 | |
| 283 | 271 | /** |
| 284 | - * @throws DeleteErrorException | |
| 272 | + * Deletes all items. | |
| 273 | + * | |
| 274 | + * @param string $customsDeclarationId Customs Declaration ID. | |
| 275 | + * @return void | |
| 285 | 276 | */ |
| 286 | 277 | private function deleteItems( string $customsDeclarationId ): void { |
| 287 | 278 | $items = $this->getItemsByCustomsDeclarationId( $customsDeclarationId ); |
| 288 | 279 | |
| @@ -297,9 +288,10 @@ | ||
| 297 | 288 | |
| 298 | 289 | /** |
| 299 | 290 | * Completely deletes Customs Declaration with all its items. |
| 300 | 291 | * |
| 301 | - * @throws DeleteErrorException | |
| 292 | + * @param string $orderId Order ID. | |
| 293 | + * @return void | |
| 302 | 294 | */ |
| 303 | 295 | public function delete( string $orderId ): void { |
| 304 | 296 | $customsDeclarationId = $this->getIdByOrderNumber( $orderId ); |
| 305 | 297 | |