1 <?php
2 3 4
5 namespace Team3\PayU\Serializer;
6
7 use Psr\Log\LoggerInterface;
8 use Team3\PayU\Order\Model\Buyer\DeliveryInterface;
9 use Team3\PayU\Order\Model\Buyer\InvoiceInterface;
10 use Team3\PayU\Order\Model\OrderInterface;
11 use Team3\PayU\Order\Model\Products\ProductCollectionInterface;
12 use Team3\PayU\Order\Model\ShippingMethods\ShippingMethodCollectionInterface;
13
14 class GroupsSpecifier implements GroupsSpecifierInterface
15 {
16 const DEFAULT_GROUP = 'Default';
17 const BUYER_GROUP = 'buyer';
18 const INVOICE_GROUP = 'invoice';
19 const DELIVERY_GROUP = 'delivery';
20 const SHIPPING_METHODS_GROUP = 'shippingMethods';
21 const PRODUCT_COLLECTION_GROUP = 'products';
22
23 24 25
26 private $groups;
27
28 29 30
31 private $logger;
32
33 34 35
36 public function __construct(LoggerInterface $logger)
37 {
38 $this->logger = $logger;
39 }
40
41 42 43 44 45 46 47
48 public function specifyGroups(OrderInterface $order)
49 {
50 $this->groups = [self::DEFAULT_GROUP];
51
52 $this->checkBuyer($order);
53 $this->checkShippingMethods($order->getShippingMethodCollection());
54 $this->checkProducts($order->getProductCollection());
55
56 $this->logSpecifiedGroups($order);
57
58 return $this->groups;
59 }
60
61 62 63 64 65
66 private function checkBuyer(OrderInterface $order)
67 {
68 $buyer = $order->getBuyer();
69 if ($buyer->isFilled()) {
70 $this->groups[] = self::BUYER_GROUP;
71 }
72
73 return $this
74 ->checkInvoice($buyer->getInvoice())
75 ->checkDelivery($buyer->getDelivery());
76 }
77
78 79 80 81 82
83 private function checkInvoice(InvoiceInterface $invoice)
84 {
85 if ($invoice->isFilled()) {
86 $this->groups[] = self::INVOICE_GROUP;
87 }
88
89 return $this;
90 }
91
92 93 94 95 96
97 private function checkDelivery(DeliveryInterface $delivery)
98 {
99 if ($delivery->isFilled()) {
100 $this->groups[] = self::DELIVERY_GROUP;
101 }
102
103 return $this;
104 }
105
106 107 108 109 110
111 private function checkShippingMethods(
112 ShippingMethodCollectionInterface $shippingMethodCollection
113 ) {
114 if ($shippingMethodCollection->isFilled()) {
115 $this->groups[] = self::SHIPPING_METHODS_GROUP;
116 }
117
118 return $this;
119 }
120
121 122 123 124 125
126 private function checkProducts(
127 ProductCollectionInterface $productCollection
128 ) {
129 if ($productCollection->isFilled()) {
130 $this->groups[] = self::PRODUCT_COLLECTION_GROUP;
131 }
132
133 return $this;
134 }
135
136 137 138
139 private function logSpecifiedGroups(OrderInterface $order)
140 {
141 $this
142 ->logger
143 ->debug(sprintf(
144 'Serialization groups for order %s were specified to %s',
145 $order->getOrderId(),
146 print_r($this->groups, true)
147 ));
148 }
149 }
150