1 | <?php |
||
10 | class B2bConfigurableAddOrReplaceStrategy extends ConfigurableAddOrReplaceStrategy |
||
11 | { |
||
12 | /** |
||
13 | * Save state about exists Billing Address value in entity or not. |
||
14 | * Value is updated in function beforeProcessEntity and used |
||
15 | * in function afterProcessEntity |
||
16 | * |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $isBillingAddress = true; |
||
20 | |||
21 | /** |
||
22 | * Save state about exists Shipping Address value in entity or not. |
||
23 | * Value is updated in function beforeProcessEntity and used |
||
24 | * in function afterProcessEntity |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $isShippingAddress = true; |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | protected function beforeProcessEntity($entity) |
||
34 | { |
||
35 | /** @var B2bCustomer $entity */ |
||
36 | $entity = parent::beforeProcessEntity($entity); |
||
37 | $this->checkEmptyAddresses($entity); |
||
38 | |||
39 | return $entity; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | protected function afterProcessEntity($entity) |
||
46 | { |
||
47 | /** @var B2bCustomer $entity */ |
||
48 | $entity = parent::afterProcessEntity($entity); |
||
49 | $this->clearEmptyAddresses($entity); |
||
50 | |||
51 | $this->guessRegion($entity->getBillingAddress()); |
||
52 | $this->guessRegion($entity->getShippingAddress()); |
||
53 | |||
54 | return $entity; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param B2bCustomer $entity |
||
59 | */ |
||
60 | protected function checkEmptyAddresses(B2bCustomer $entity) |
||
61 | { |
||
62 | if (!$entity->getBillingAddress()) { |
||
63 | $this->isBillingAddress = false; |
||
64 | } |
||
65 | |||
66 | if (!$entity->getShippingAddress()) { |
||
67 | $this->isShippingAddress = false; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param B2bCustomer $entity |
||
73 | */ |
||
74 | protected function clearEmptyAddresses(B2bCustomer $entity) |
||
75 | { |
||
76 | if (!$this->isBillingAddress) { |
||
77 | $entity->setBillingAddress(null); |
||
78 | } |
||
79 | |||
80 | if (!$this->isShippingAddress) { |
||
81 | $entity->setShippingAddress(null); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param Address $address |
||
87 | */ |
||
88 | protected function guessRegion($address) |
||
108 | } |
||
109 |