Skip to content

Commit

Permalink
2.2.16 - serializers - check objects before using them
Browse files Browse the repository at this point in the history
  • Loading branch information
bnayalivne committed Feb 19, 2018
1 parent d2641e7 commit a3b5af6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Model/Api/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue)
*/
public function getVersion()
{
return '2.2.15';
return '2.2.16';
}

/**
Expand Down
13 changes: 10 additions & 3 deletions Serializer/AddressSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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
Expand Down
37 changes: 29 additions & 8 deletions Serializer/CustomerSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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()){
Expand All @@ -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(),
Expand All @@ -72,12 +84,21 @@ 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()
];

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()
]);
}
}
41 changes: 28 additions & 13 deletions Serializer/OrderSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand Down Expand Up @@ -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)
];
}

Expand All @@ -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();
Expand All @@ -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(),
Expand Down
25 changes: 1 addition & 24 deletions Serializer/ProductSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lib-libxml": "*"
},
"type": "magento2-module",
"version": "2.2.15",
"version": "2.2.16",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Remarkety_Mgconnector" setup_version="2.2.15">
<module name="Remarkety_Mgconnector" setup_version="2.2.16">
</module>
</config>

0 comments on commit a3b5af6

Please sign in to comment.