From a3b5af6f5b47c5163087ab81e0acfa96a30f535b Mon Sep 17 00:00:00 2001 From: bnayal Date: Mon, 19 Feb 2018 10:35:01 +0200 Subject: [PATCH] 2.2.16 - serializers - check objects before using them --- Model/Api/Data.php | 2 +- Serializer/AddressSerializer.php | 13 +++++++--- Serializer/CustomerSerializer.php | 37 ++++++++++++++++++++++------ Serializer/OrderSerializer.php | 41 +++++++++++++++++++++---------- Serializer/ProductSerializer.php | 25 +------------------ composer.json | 2 +- etc/module.xml | 2 +- 7 files changed, 71 insertions(+), 51 deletions(-) diff --git a/Model/Api/Data.php b/Model/Api/Data.php index 9ca1d28..4699bac 100644 --- a/Model/Api/Data.php +++ b/Model/Api/Data.php @@ -1241,7 +1241,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue) */ public function getVersion() { - return '2.2.15'; + return '2.2.16'; } /** diff --git a/Serializer/AddressSerializer.php b/Serializer/AddressSerializer.php index c0016fc..6c99325 100644 --- a/Serializer/AddressSerializer.php +++ b/Serializer/AddressSerializer.php @@ -25,7 +25,14 @@ public function __construct( * @return array */ public function serialize($address){ - $country = $this->_countryFactory->create()->loadByCode($address->getCountryId()); + $countryCode = $address->getCountryId(); + $countryName = null; + if(!empty($countryCode)){ + $country = $this->_countryFactory->create()->loadByCode($countryCode); + if(!empty($country)){ + $countryName = $country->getName(); + } + } $region = null; $regionStr = null; if($address instanceof \Magento\Sales\Model\Order\Address){ @@ -45,8 +52,8 @@ public function serialize($address){ 'last_name' => $address->getLastname(), 'city' => $address->getCity(), 'street' => implode(PHP_EOL, $address->getStreet()), - 'country_code' => $address->getCountryId(), - 'country' => $country->getName(), + 'country_code' => $countryCode, + 'country' => $countryName, 'zip' => $address->getPostcode(), 'phone' => $address->getTelephone(), 'region' => $regionStr diff --git a/Serializer/CustomerSerializer.php b/Serializer/CustomerSerializer.php index 3125ba4..739dfce 100644 --- a/Serializer/CustomerSerializer.php +++ b/Serializer/CustomerSerializer.php @@ -20,17 +20,20 @@ class CustomerSerializer private $addressSerializer; private $customerGroupRepository; private $request; + private $logger; public function __construct( Subscriber $subscriber, AddressSerializer $addressSerializer, CustomerGroupRepository $customerGroupRepository, - RequestInterface $request + RequestInterface $request, + \Psr\Log\LoggerInterface $logger = null ) { $this->subscriber = $subscriber; $this->addressSerializer = $addressSerializer; $this->customerGroupRepository = $customerGroupRepository; $this->request = $request; + $this->logger = $logger; } public function serialize(\Magento\Customer\Api\Data\CustomerInterface $customer){ if ($this->request->getParam('is_subscribed', false)) { @@ -45,11 +48,17 @@ public function serialize(\Magento\Customer\Api\Data\CustomerInterface $customer $groups = []; if(!empty($customer->getGroupId())){ - $group = $this->customerGroupRepository->getById($customer->getGroupId()); - $groups[] = [ - 'id' => $group->getId(), - 'name' => $group->getCode(), - ]; + try { + $group = $this->customerGroupRepository->getById($customer->getGroupId()); + if ($group) { + $groups[] = [ + 'id' => $group->getId(), + 'name' => $group->getCode(), + ]; + } + } catch (\Exception $ex){ + $this->logError($ex); + } } $gender = null; switch($customer->getGender()){ @@ -61,7 +70,10 @@ public function serialize(\Magento\Customer\Api\Data\CustomerInterface $customer break; } - $address = $customer->getAddresses(); + $addresses = $customer->getAddresses(); + if(!empty($addresses)){ + $address = array_pop($addresses); + } $customerInfo = [ 'id' => (int)$customer->getId(), 'email' => $customer->getEmail(), @@ -72,7 +84,7 @@ public function serialize(\Magento\Customer\Api\Data\CustomerInterface $customer 'created_at' => $created_at->format(\DateTime::ATOM ), 'updated_at' => $updated_at->format(\DateTime::ATOM ), 'guest' => false, - 'default_address' => empty($address) ? null : $this->addressSerializer->serialize($address[0]), + 'default_address' => empty($address) ? null : $this->addressSerializer->serialize($address), 'groups' => $groups, 'gender' => $gender, 'birthdate' => $customer->getDob() @@ -80,4 +92,13 @@ public function serialize(\Magento\Customer\Api\Data\CustomerInterface $customer return $customerInfo; } + + protected function logError(\Exception $exception){ + $this->logger->error("Remarkety:".self::class." - " . $exception->getMessage(), [ + 'message' => $exception->getMessage(), + 'line' => $exception->getLine(), + 'file' => $exception->getFile(), + 'trace' => $exception->getTraceAsString() + ]); + } } diff --git a/Serializer/OrderSerializer.php b/Serializer/OrderSerializer.php index daab755..2382f77 100644 --- a/Serializer/OrderSerializer.php +++ b/Serializer/OrderSerializer.php @@ -35,7 +35,21 @@ public function __construct( } public function serialize(\Magento\Sales\Model\Order $order){ - $status = $this->statusCollection->getItemByColumnValue('status', $order->getStatus()); + //find order status + $statusVal = $order->getStatus(); + $status = [ + 'code' => 'unknown', + 'name' => 'unknown' + ]; + if(!empty($statusVal)){ + $statusObj = $this->statusCollection->getItemByColumnValue('status', $statusVal); + if(!empty($statusObj)){ + $status = [ + 'code' => $statusObj->getStatus(), + 'name' => $statusObj->getLabel() + ]; + } + } /** * @var $items \Magento\Sales\Model\Order\Item[] */ @@ -71,23 +85,22 @@ public function serialize(\Magento\Sales\Model\Order $order){ $created_at = new \DateTime($order->getCreatedAt()); $updated_at = new \DateTime($order->getUpdatedAt()); - $customerId = $order->getCustomerId(); if(!$order->getCustomerIsGuest()){ $customer = $this->customerRepository->getById($order->getCustomerId()); $customerInfo = $this->customerSerializer->serialize($customer); } else { $checkSubscriber = $this->subscriber->loadByEmail($order->getCustomerEmail()); - + $billingAddress = $order->getBillingAddress(); $customerInfo = [ 'accepts_marketing' => $checkSubscriber->isSubscribed(), 'email' => $order->getCustomerEmail(), - 'title' => $order->getBillingAddress()->getPrefix(), - 'first_name' => $order->getBillingAddress()->getFirstname(), - 'last_name' => $order->getBillingAddress()->getLastname(), + 'title' => empty($billingAddress) ? null : $billingAddress->getPrefix(), + 'first_name' => empty($billingAddress) ? null : $billingAddress->getFirstname(), + 'last_name' => empty($billingAddress) ? null : $billingAddress->getLastname(), 'created_at' => $created_at->format(\DateTime::ATOM ), 'updated_at' => $created_at->format(\DateTime::ATOM ), 'guest' => true, - 'default_address' => $this->addressSerializer->serialize($order->getBillingAddress()) + 'default_address' => empty($billingAddress) ? null : $this->addressSerializer->serialize($billingAddress) ]; } @@ -109,9 +122,14 @@ public function serialize(\Magento\Sales\Model\Order $order){ } } + $paymentMethodTitle = null; $payment = $order->getPayment(); - $method = $payment->getMethodInstance(); - $paymentMethodTitle = $method->getTitle(); + if(!empty($payment)){ + $method = $payment->getMethodInstance(); + if(!empty($method)){ + $paymentMethodTitle = $method->getTitle(); + } + } $discount_codes = []; $coupon = $order->getCouponCode(); @@ -132,10 +150,7 @@ public function serialize(\Magento\Sales\Model\Order $order){ 'discount_codes' => $discount_codes, 'payment_method' => $paymentMethodTitle, 'note' => $order->getCustomerNote(), - 'status' => [ - 'code' => $status->getStatus(), - 'name' => $status->getLabel() - ], + 'status' => $status, 'subtotal_price' => (float)$order->getSubtotal(), 'total_discounts' => (float)$order->getDiscountAmount(), 'total_price' => (float)$order->getGrandTotal(), diff --git a/Serializer/ProductSerializer.php b/Serializer/ProductSerializer.php index 0ca8b13..42718b1 100644 --- a/Serializer/ProductSerializer.php +++ b/Serializer/ProductSerializer.php @@ -54,6 +54,7 @@ public function loadProduct($product_id, $store_id = null){ public function serialize(ProductInterface $product, $storeId){ $parent_id = null; + $parentProduct = null; if($product->getTypeId() == 'simple'){ $parent_id = $this->dataHelper->getParentId($product->getId()); if(!empty($parent_id)) { @@ -183,30 +184,6 @@ public function serialize(ProductInterface $product, $storeId){ $data['parent_id'] = $parent_id; } return $data; - - /** - * $data = array( - 'id' => $product->getId(), - 'sku' => $product->getSku(), - 'title' => $rmCore->getProductName($product, $storeId), - 'body_html' => '', - 'categories' => $categories, - 'created_at' => $product->getCreatedAt(), - 'updated_at' => $product->getUpdatedAt(), - 'images' => $this->getProductImages($product), - 'enabled' => $enabled, - 'price' => $price, - 'special_price' => $special_price, - 'url' => $url, - 'parent_id' => $rmCore->getProductParentId($product), - 'variants' => array( - array( - 'inventory_quantity' => $stocklevel, - 'price' => $price - ) - ) - ); - */ } public function getParentId($id) diff --git a/composer.json b/composer.json index 9bf9a04..91f2649 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "lib-libxml": "*" }, "type": "magento2-module", - "version": "2.2.15", + "version": "2.2.16", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/etc/module.xml b/etc/module.xml index 21fbd4b..0fab0a1 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - +