Advertisement
Guest User

Untitled

a guest
May 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1.     public function ajaxGetAddressesWithDataByCustomerAndDistributorAction(int $customerId, int $distributorId) {
  2.  
  3.         //serializer construction
  4.         $objectNormalizer = new ObjectNormalizer();
  5.         $objectNormalizer->setCircularReferenceLimit(2);
  6.         $objectNormalizer->setCircularReferenceHandler(function ($object) {
  7.             return $object->getId();
  8.         });
  9.         $objectNormalizer->setIgnoredAttributes(['subscriber']);
  10.  
  11.         $serializer = new Serializer([new DateTimeNormalizer(), $objectNormalizer], []);
  12.  
  13.         $doctrine = $this->getDoctrine();
  14.  
  15.         //get data to make retrieving keys
  16.         $customer = $doctrine->getRepository(Customer::class)->find($customerId);
  17.         $distributorReference = $doctrine->getManager()->getReference(Distributor::class, $distributorId);
  18.         $addresses = $doctrine->getRepository(Address::class)->findBy([
  19.             'subscriber' => $customer->getSubscriber()
  20.         ]);
  21.  
  22.         $addressesToSend = $serializer->normalize($addresses, 'json');
  23.  
  24.         //each address => attach customerData to them
  25.         foreach ($addressesToSend as &$address) {
  26.             //keys to retrieve data in both customerData tables
  27.             $keys = [
  28.                 'customer' => $customer,
  29.                 'distributor' => $distributorReference,
  30.                 'address' => $doctrine->getManager()->getReference(Address::class, $address['id'])
  31.                 //, 'acceptedByDistributor' => true
  32.             ];
  33.  
  34.             //get customerData
  35.             $billingData = $doctrine->getRepository(CustomerDistributorBillingData::class)->findOneBy($keys);
  36.             $deliveryData = $doctrine->getRepository(CustomerDistributorDeliveryData::class)->findOneBy($keys);
  37.  
  38.             //set customerData
  39.             $address['clientCode'] = ($billingData) ? $billingData->getClientCode() : null;
  40.             $address['deliveryDays'] = ($deliveryData) ? $deliveryData->getDeliveryDays() : null;
  41.             $address['both'] = $billingData && $deliveryData;
  42.             $address['billing'] =  $billingData && !$deliveryData;
  43.             $address['delivery'] = $deliveryData && !$billingData;
  44.         }
  45.  
  46.         return new JsonResponse($addressesToSend);
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement