| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Order |
| 4 |
* |
| 5 |
* @package Packetery\Module\EntityFactory |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Entity; |
| 13 |
use Packetery\Core\Entity\Address; |
| 14 |
use Packetery\Core\Helper; |
| 15 |
use Packetery\Module\Carrier; |
| 16 |
use Packetery\Module\Options\Provider; |
| 17 |
use WC_Order; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Order |
| 21 |
* |
| 22 |
* @package Packetery\Module\EntityFactory |
| 23 |
*/ |
| 24 |
class Builder { |
| 25 |
|
| 26 |
/** |
| 27 |
* Options provider. |
| 28 |
* |
| 29 |
* @var Provider Options provider. |
| 30 |
*/ |
| 31 |
private $optionsProvider; |
| 32 |
|
| 33 |
/** |
| 34 |
* Carrier repository. |
| 35 |
* |
| 36 |
* @var Carrier\Repository Carrier repository. |
| 37 |
*/ |
| 38 |
private $carrierRepository; |
| 39 |
|
| 40 |
/** |
| 41 |
* Order constructor. |
| 42 |
* |
| 43 |
* @param Provider $optionsProvider Options Provider. |
| 44 |
* @param Carrier\Repository $carrierRepository Carrier repository. |
| 45 |
*/ |
| 46 |
public function __construct( |
| 47 |
Provider $optionsProvider, |
| 48 |
Carrier\Repository $carrierRepository |
| 49 |
) { |
| 50 |
$this->optionsProvider = $optionsProvider; |
| 51 |
$this->carrierRepository = $carrierRepository; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Creates common order entity from WC_Order. |
| 56 |
* |
| 57 |
* @param WC_Order $order WC_Order. |
| 58 |
* @param Entity\Order $partialOrder Partial order. |
| 59 |
* |
| 60 |
* @return Entity\Order|null |
| 61 |
*/ |
| 62 |
public function finalize( WC_Order $order, Entity\Order $partialOrder ): ?Entity\Order { |
| 63 |
$orderData = $order->get_data(); |
| 64 |
$contactInfo = ( $order->has_shipping_address() ? $orderData['shipping'] : $orderData['billing'] ); |
| 65 |
|
| 66 |
if ( null === $partialOrder->getCarrierId() ) { |
| 67 |
return null; |
| 68 |
} |
| 69 |
|
| 70 |
$partialOrder->setName( $contactInfo['first_name'] ); |
| 71 |
$partialOrder->setSurname( $contactInfo['last_name'] ); |
| 72 |
$partialOrder->setEshop( $this->optionsProvider->get_sender() ); |
| 73 |
$partialOrder->setWeight( Helper::simplifyWeight( $partialOrder->getWeight() ) ); |
| 74 |
$partialOrder->setValue( (float) $order->get_total( 'raw' ) ); |
| 75 |
|
| 76 |
$address = $partialOrder->getDeliveryAddress(); |
| 77 |
if ( null === $address ) { |
| 78 |
$partialOrder->setAddressValidated( false ); |
| 79 |
$address = new Address( $contactInfo['address_1'], $contactInfo['city'], $contactInfo['postcode'] ); |
| 80 |
} |
| 81 |
|
| 82 |
$partialOrder->setDeliveryAddress( $address ); |
| 83 |
|
| 84 |
// Shipping address phone is optional. |
| 85 |
$partialOrder->setPhone( $orderData['billing']['phone'] ); |
| 86 |
if ( ! empty( $contactInfo['phone'] ) ) { |
| 87 |
$partialOrder->setPhone( $contactInfo['phone'] ); |
| 88 |
} |
| 89 |
// Additional address information. |
| 90 |
if ( ! empty( $contactInfo['address_2'] ) ) { |
| 91 |
$partialOrder->setNote( $contactInfo['address_2'] ); |
| 92 |
} |
| 93 |
|
| 94 |
$partialOrder->setEmail( $orderData['billing']['email'] ); |
| 95 |
$codMethod = $this->optionsProvider->getCodPaymentMethod(); |
| 96 |
if ( $orderData['payment_method'] === $codMethod ) { |
| 97 |
$partialOrder->setCod( $partialOrder->getValue() ); |
| 98 |
} |
| 99 |
$partialOrder->setSize( $partialOrder->getSize() ); |
| 100 |
|
| 101 |
if ( $partialOrder->isExternalCarrier() ) { |
| 102 |
$carrier = $this->carrierRepository->getById( (int) $partialOrder->getCarrierId() ); |
| 103 |
$partialOrder->setCarrier( $carrier ); |
| 104 |
} |
| 105 |
|
| 106 |
$partialOrder->setCurrency( $order->get_currency() ); |
| 107 |
|
| 108 |
return $partialOrder; |
| 109 |
} |
| 110 |
} |
| 111 |
|