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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @copyright Vertex. All rights reserved. https://www.vertexinc.com/
* @author Mediotype https://www.mediotype.com/
*/
namespace Vertex\Tax\Model\Repository;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Vertex\Tax\Model\Data\CustomerCode;
use Vertex\Tax\Model\Data\CustomerCodeFactory;
use Vertex\Tax\Model\Registry\CustomerCodeRegistry;
use Vertex\Tax\Model\ResourceModel\CustomerCode as ResourceModel;
/**
* Repository of Vertex Customer Codes
*/
class CustomerCodeRepository
{
/** @var ResourceModel */
private $resourceModel;
/** @var CustomerCodeFactory */
private $factory;
/** @var CustomerCodeRegistry */
private $registry;
/**
* @param ResourceModel $resourceModel
* @param CustomerCodeFactory $factory
* @param CustomerCodeRegistry $registry
*/
public function __construct(
ResourceModel $resourceModel,
CustomerCodeFactory $factory,
CustomerCodeRegistry $registry
) {
$this->resourceModel = $resourceModel;
$this->factory = $factory;
$this->registry = $registry;
}
/**
* Save a Customer Code
*
* @param CustomerCode $customerCode
* @return $this
* @throws AlreadyExistsException
* @throws CouldNotSaveException
*/
public function save(CustomerCode $customerCode)
{
// Exit early if code is already in the registry
$registered = $this->registry->get($customerCode->getCustomerId());
if ($registered === $customerCode->getCustomerCode()) {
return $this;
}
try {
$this->resourceModel->save($customerCode);
$this->registry->set($customerCode->getCustomerId(), $customerCode->getCustomerCode());
} catch (AlreadyExistsException $e) {
throw $e;
} catch (\Exception $originalException) {
throw new CouldNotSaveException(__('Unable to save Customer Code'), $originalException);
}
return $this;
}
/**
* Delete a Customer Code
*
* @param CustomerCode $customerCode
* @return $this
* @throws CouldNotDeleteException
*/
public function delete(CustomerCode $customerCode)
{
return $this->deleteByCustomerId($customerCode->getCustomerId());
}
/**
* Delete a Customer Code given a Customer ID
*
* @param int $customerId
* @return $this
* @throws CouldNotDeleteException
*/
public function deleteByCustomerId($customerId)
{
/** @var CustomerCode $customerCode */
$customerCode = $this->factory->create();
$customerCode->setId($customerId);
try {
$this->resourceModel->delete($customerCode);
$this->registry->delete($customerId);
} catch (\Exception $originalException) {
throw new CouldNotDeleteException(__('Unable to delete Customer Code'), $originalException);
}
return $this;
}
/**
* Retrieve a Customer Code given a Customer ID
*
* @param int $customerId
* @return CustomerCode
* @throws NoSuchEntityException
*/
public function getByCustomerId($customerId)
{
/** @var CustomerCode $customerCode */
$customerCode = $this->factory->create();
$registered = $this->registry->get($customerId);
if ($registered === null) {
throw NoSuchEntityException::singleField('customerId', $customerId);
}
if ($registered !== false) {
$customerCode->setCustomerId($customerId);
$customerCode->setCustomerCode($registered);
return $customerCode;
}
$this->resourceModel->load($customerCode, $customerId);
if (!$customerCode->getId()) {
$this->registry->set($customerId, null);
throw NoSuchEntityException::singleField('customerId', $customerId);
}
$this->registry->set($customerCode->getCustomerId(), $customerCode->getCustomerCode());
return $customerCode;
}
/**
* Retrieve an array of Customer Code's indexed by Customer ID
*
* @param int[] $customerIds
* @return CustomerCode[] Indexed by Customer ID
*/
public function getListByCustomerIds(array $customerIds)
{
$unregisteredIds = [];
$registryCustomerCodes = [];
foreach ($customerIds as $customerId) {
$registered = $this->registry->get($customerId);
if ($registered === false) {
$unregisteredIds[] = $customerId;
} else {
$customerCode = $this->factory->create();
$customerCode->setCustomerId($customerId);
$customerCode->setCustomerCode($registered);
$registryCustomerCodes[$customerId] = $customerCode;
}
}
$dbCustomerCodes = [];
if (!empty($unregisteredIds)) {
$dbCustomerCodes = $this->resourceModel->getArrayByCustomerIds($unregisteredIds);
}
foreach ($dbCustomerCodes as $dbCustomerCode) {
$this->registry->set($dbCustomerCode->getCustomerId(), $dbCustomerCode->getCustomerCode());
}
return array_replace($dbCustomerCodes, $registryCustomerCodes);
}
}