Skip to content

Commit

Permalink
refactor: remove deprecated code, null references and use yoda (#49)
Browse files Browse the repository at this point in the history
INT-189
  • Loading branch information
joerivanveen authored Nov 30, 2023
1 parent 9773a3a commit 9a9c378
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/Service/Consignment/ConsignmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use MyParcelNL\Sdk\src\Model\MyParcelCustomsItem;
use MyParcelNL\Sdk\src\Support\Str;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Document\DocumentCollection;
use Shopware\Core\Checkout\Document\DocumentEntity;
use Shopware\Core\Checkout\Document\DocumentGenerator\InvoiceGenerator;
use Shopware\Core\Checkout\Document\Renderer\InvoiceRenderer;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Api\Context\SystemSource;
Expand Down Expand Up @@ -169,21 +171,21 @@ public function createConsignment(
?int $packageType
): AbstractConsignment
{
if ($orderEntity->getOrderCustomer() === null) {
if (null === $orderEntity->getOrderCustomer()) {
throw new RuntimeException('Could not get a customer');
}
if (
$orderEntity->getDeliveries() === null ||
$orderEntity->getDeliveries()->first() === null ||
$orderEntity->getDeliveries()->first()->getShippingOrderAddress() === null
null === $orderEntity->getDeliveries()
|| null === $orderEntity->getDeliveries()->first()
|| null === $orderEntity->getDeliveries()->first()->getShippingOrderAddress()
) {
throw new \RuntimeException('Could not get a shipping address');
}

$shippingAddress = $orderEntity->getDeliveries()->first()->getShippingOrderAddress();

if ($shippingAddress === null ||
$shippingAddress->getCountry() === null
if (null === $shippingAddress
|| null === $shippingAddress->getCountry()
) {
throw new \RuntimeException('Shipping address is not properly formatted');
}
Expand All @@ -192,7 +194,7 @@ public function createConsignment(

$shippingOptions = $this->shippingOptionsService->getShippingOptionsForOrder($orderEntity, $context);

if ($shippingOptions === null) {
if (null === $shippingOptions) {
throw new \RuntimeException('No valid Shipping Options found');
}

Expand All @@ -212,9 +214,10 @@ public function createConsignment(
->setEmail($orderEntity->getOrderCustomer()->getEmail());

//Set invoice number to the latest invoice document number or order number if none is available
$invoice = $orderEntity->getDocuments()->filter(function ($document) {
$documents = $orderEntity->getDocuments() ?? new DocumentCollection();
$invoice = $documents->filter(function ($document) {
/** @var DocumentEntity $document */
return $document->getDocumentType()->getTechnicalName() === InvoiceGenerator::INVOICE;
return $document->getDocumentType() && InvoiceRenderer::TYPE === $document->getDocumentType()->getTechnicalName();
})->last();

if ($invoice instanceof DocumentEntity) {
Expand All @@ -224,7 +227,7 @@ public function createConsignment(
}
$consignment->setInvoice($invoiceNumber);

if ($shippingOptions->getDeliveryDate() !== null) {
if (null !== $shippingOptions->getDeliveryDate()) {

$shippingDate = $shippingOptions->getDeliveryDate()->format('Y-m-d');

Expand All @@ -243,15 +246,15 @@ public function createConsignment(
foreach ($orderEntity->getLineItems() as $lineItem) {

$customsItem = new MyParcelCustomsItem();
if ($lineItem->getProduct()->getWeight()) {
if ($lineItem->getProduct() && $lineItem->getProduct()->getWeight()) {
$customsItem->setWeight($lineItem->getProduct()->getWeight() * 1000);
} else {
$customsItem->setWeight(0.01);
}
$customsItem->setAmount($lineItem->getQuantity());
$customsItem->setDescription($lineItem->getLabel());
$customsItem->setItemValue($lineItem->getUnitPrice() * 100);// In cents
if ($this->systemConfigService->getString('MyPaShopware.config.platform') === "myparcel") {
if ('myparcel' === $this->systemConfigService->getString('MyPaShopware.config.platform')) {
$customsItem->setCountry('NL');
} else {
$customsItem->setCountry('BE');
Expand Down Expand Up @@ -349,7 +352,7 @@ public function createConsignment(
}

if ($shippingOptions->getDeliveryType() == AbstractConsignment::DELIVERY_TYPE_PICKUP) {
$consignment->setPickupLocationCode(strval($shippingOptions->getLocationId()));
$consignment->setPickupLocationCode((string) $shippingOptions->getLocationId());
$consignment->setPickupLocationName($shippingOptions->getLocationName());
$consignment->setPickupStreet($shippingOptions->getLocationStreet());
$consignment->setPickupNumber($shippingOptions->getLocationNumber());
Expand Down Expand Up @@ -389,7 +392,7 @@ private function createShipment(
): ?ShipmentEntity
{
$shipmentParameters = [
ShipmentEntity::FIELD_CONSIGNMENT_REFERENCE => $consignment->getReferenceId(),
ShipmentEntity::FIELD_CONSIGNMENT_REFERENCE => $consignment->getReferenceIdentifier(),
ShipmentEntity::FIELD_ORDER => [
ShipmentEntity::FIELD_ID => $orderEntity->getId(),
ShipmentEntity::FIELD_VERSION_ID => $orderEntity->getVersionId(),
Expand All @@ -399,7 +402,7 @@ private function createShipment(
],
];

if ($consignment->getBarcode() !== null) {
if (null !== $consignment->getBarcode()) {
$shipmentParameters[ShipmentEntity::FIELD_BAR_CODE] = $consignment->getBarcode();
$shipmentParameters[ShipmentEntity::FIELD_TRACK_AND_TRACE_URL] = $consignment->getBarcodeUrl(
$consignment->getBarcode(),
Expand All @@ -408,7 +411,7 @@ private function createShipment(
);

// Add track and trace to the custom fields
$customFields = json_decode($orderEntity->getCustomFields()['my_parcel'], true) ?? null;
$customFields = json_decode($orderEntity->getCustomFields()['my_parcel'], true);
$trackAndTrace = $customFields['track_and_trace'] ?? [];

$trackAndTrace[] = [
Expand Down Expand Up @@ -538,9 +541,8 @@ public function createConsignments( //NOSONAR
$consignment = $foundConsignments[0];
}

if ($consignment !== null) {
$createdShipment = $this->createShipment($shipment['context'], $shipment['order'], $shipment['shippingOptionId'], $consignment);
$shipments[] = $createdShipment;
if (null !== $consignment) {
$this->createShipment($shipment['context'], $shipment['order'], $shipment['shippingOptionId'], $consignment);
}
}
}
Expand Down

0 comments on commit 9a9c378

Please sign in to comment.