-
Notifications
You must be signed in to change notification settings - Fork 83
/
OrderHandler.php
121 lines (99 loc) · 3 KB
/
OrderHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Oro\Bundle\TaxBundle\OrderTax\ContextHandler;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\OrderBundle\Entity\Order;
use Oro\Bundle\TaxBundle\Entity\CustomerTaxCode;
use Oro\Bundle\TaxBundle\Entity\Repository\CustomerTaxCodeRepository;
use Oro\Bundle\TaxBundle\Event\ContextEvent;
use Oro\Bundle\TaxBundle\Model\Taxable;
use Oro\Bundle\TaxBundle\Model\TaxCodeInterface;
class OrderHandler
{
/**
* @var DoctrineHelper
*/
protected $doctrineHelper;
/**
* @var TaxCodeInterface[]
*/
protected $taxCodes = [];
/**
* @param DoctrineHelper $doctrineHelper
*/
public function __construct(DoctrineHelper $doctrineHelper)
{
$this->doctrineHelper = $doctrineHelper;
}
/**
* @param ContextEvent $contextEvent
*/
public function onContextEvent(ContextEvent $contextEvent)
{
$order = $contextEvent->getMappingObject();
$context = $contextEvent->getContext();
if (!$order instanceof Order) {
return;
}
$context->offsetSet(Taxable::ACCOUNT_TAX_CODE, $this->getCustomerTaxCode($order));
}
/**
* @param Order $order
* @return null|string
*/
protected function getCustomerTaxCode(Order $order)
{
$cacheKey = $this->getCacheTaxCodeKey(TaxCodeInterface::TYPE_ACCOUNT, $order);
$cachedTaxCode = $this->getCachedTaxCode($cacheKey);
if ($cachedTaxCode !== false) {
return $cachedTaxCode;
}
$taxCode = null;
$customer = null;
if ($order->getCustomer()) {
$taxCode = $this->getTaxCode(TaxCodeInterface::TYPE_ACCOUNT, $order->getCustomer());
}
if (!$taxCode && $order->getCustomer() && $order->getCustomer()->getGroup()) {
$taxCode = $this->getTaxCode(TaxCodeInterface::TYPE_ACCOUNT_GROUP, $order->getCustomer()->getGroup());
}
$this->taxCodes[$cacheKey] = $taxCode;
return $taxCode;
}
/**
* @param string $type
* @param object $object
* @return string|null
*/
protected function getTaxCode($type, $object)
{
$taxCode = $this->getRepository()->findOneByEntity($type, $object);
return $taxCode ? $taxCode->getCode() : null;
}
/**
* @return CustomerTaxCodeRepository
*/
protected function getRepository()
{
return $this->doctrineHelper->getEntityRepositoryForClass(CustomerTaxCode::class);
}
/**
* @param string $type
* @param Order $order
* @return string
*/
protected function getCacheTaxCodeKey($type, Order $order)
{
$id = $order->getId() ?: spl_object_hash($order);
return implode(':', [$type, $id]);
}
/**
* @param string $cacheKey
* @return null|TaxCodeInterface
*/
protected function getCachedTaxCode($cacheKey)
{
if (!array_key_exists($cacheKey, $this->taxCodes)) {
return false;
}
return $this->taxCodes[$cacheKey];
}
}