1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Customer\Repositories\CustomerRepository |
5
|
|
|
* |
6
|
|
|
* PHP version 7 |
7
|
|
|
* |
8
|
|
|
* @author Tim Wagner <[email protected]> |
9
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT |
11
|
|
|
* @link https://github.com/techdivision/import-customer |
12
|
|
|
* @link http://www.techdivision.com |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TechDivision\Import\Customer\Repositories; |
16
|
|
|
|
17
|
|
|
use TechDivision\Import\Customer\Utils\MemberNames; |
18
|
|
|
use TechDivision\Import\Customer\Utils\SqlStatementKeys; |
19
|
|
|
use TechDivision\Import\Dbal\Collection\Repositories\AbstractRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Repository implementation to load customer data. |
23
|
|
|
* |
24
|
|
|
* @author Tim Wagner <[email protected]> |
25
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
26
|
|
|
* @license https://opensource.org/licenses/MIT |
27
|
|
|
* @link https://github.com/techdivision/import-customer |
28
|
|
|
* @link http://www.techdivision.com |
29
|
|
|
*/ |
30
|
|
|
class CustomerRepository extends AbstractRepository implements CustomerRepositoryInterface |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The prepared statement to load a customer with the passed entity ID. |
35
|
|
|
* |
36
|
|
|
* @var \PDOStatement |
37
|
|
|
*/ |
38
|
|
|
protected $customerStmt; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The prepared statement to load a customer with the passed email and website ID. |
42
|
|
|
* |
43
|
|
|
* @var \PDOStatement |
44
|
|
|
*/ |
45
|
|
|
protected $customerByEmailAndWebsiteIdStmt; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The prepared statement to load a customer with the passed email and website ID. |
49
|
|
|
* |
50
|
|
|
* @var \PDOStatement |
51
|
|
|
*/ |
52
|
|
|
protected $customerByWebsiteIdAndIncrementIdStmt; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The prepared statement to load the existing customers. |
56
|
|
|
* |
57
|
|
|
* @var \PDOStatement |
58
|
|
|
*/ |
59
|
|
|
protected $customersStmt; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var PDOStatement |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
protected $directoryCountryRegionsStatement; |
65
|
|
|
/** |
66
|
|
|
* Initializes the repository's prepared statements. |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function init() |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
// initialize the prepared statements |
74
|
|
|
$this->customerStmt = |
75
|
|
|
$this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::CUSTOMER)); |
76
|
|
|
$this->customerByEmailAndWebsiteIdStmt = |
77
|
|
|
$this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::CUSTOMER_BY_EMAIL_AND_WEBSITE_ID)); |
78
|
|
|
$this->customerByWebsiteIdAndIncrementIdStmt = |
79
|
|
|
$this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::CUSTOMER_BY_WEBSITE_ID_AND_INCREMET_ID)); |
80
|
|
|
$this->customersStmt = |
81
|
|
|
$this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::CUSTOMERS)); |
82
|
|
|
$this->directoryCountryRegionsStatement = |
83
|
|
|
$this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::SELECT_DIRECTORY_COUNTRY_REGIONS)); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return's the available customers. |
88
|
|
|
* |
89
|
|
|
* @return array The available customers |
90
|
|
|
*/ |
91
|
|
|
public function findAll() |
92
|
|
|
{ |
93
|
|
|
// load and return the available customers |
94
|
|
|
$this->customersStmt->execute(); |
95
|
|
|
return $this->customersStmt->fetchAll(\PDO::FETCH_ASSOC); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return's the customer with the passed entity ID. |
100
|
|
|
* |
101
|
|
|
* @param integer $id The entity ID of the customer to return |
102
|
|
|
* |
103
|
|
|
* @return array|null The customer |
104
|
|
|
*/ |
105
|
|
|
public function load($id) |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
// if not, try to load the customer with the passed entity ID |
109
|
|
|
$this->customerStmt->execute(array(MemberNames::ENTITY_ID => $id)); |
110
|
|
|
return $this->customerStmt->fetch(\PDO::FETCH_ASSOC); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return's the customer with the passed email and website ID. |
115
|
|
|
* |
116
|
|
|
* @param string $email The email of the customer to return |
117
|
|
|
* @param string $websiteId The website ID of the customer to return |
118
|
|
|
* |
119
|
|
|
* @return array|null The customer |
120
|
|
|
*/ |
121
|
|
|
public function findOneByEmailAndWebsiteId($email, $websiteId) |
122
|
|
|
{ |
123
|
|
|
|
124
|
|
|
// initialize the params |
125
|
|
|
$params = array( |
126
|
|
|
MemberNames::EMAIL => $email, |
127
|
|
|
MemberNames::WEBSITE_ID => $websiteId |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
// if not, try to load the customer with the passed email and website ID |
131
|
|
|
$this->customerByEmailAndWebsiteIdStmt->execute($params); |
132
|
|
|
return $this->customerByEmailAndWebsiteIdStmt->fetch(\PDO::FETCH_ASSOC); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Return's the customer with the passed email, website ID and increment id. |
137
|
|
|
* |
138
|
|
|
* @param string $websiteId The website ID of the customer to return |
139
|
|
|
* @param string $increment_id The website ID of the customer to return |
140
|
|
|
* |
141
|
|
|
* @return array|null The customer |
142
|
|
|
*/ |
143
|
|
|
public function loadCustomerByWebsiteIdAndIncrementId($websiteId, $increment_id) |
144
|
|
|
{ |
145
|
|
|
|
146
|
|
|
// initialize the params |
147
|
|
|
$params = array( |
148
|
|
|
MemberNames::WEBSITE_ID => $websiteId, |
149
|
|
|
MemberNames::INCREMENT_ID => $increment_id |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
// if not, try to load the customer with the passed email and website ID |
153
|
|
|
$this->customerByWebsiteIdAndIncrementIdStmt->execute($params); |
154
|
|
|
return $this->customerByWebsiteIdAndIncrementIdStmt->fetch(\PDO::FETCH_ASSOC); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Return's all country regions from directory |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function findDirectoryCountryRegions() |
163
|
|
|
{ |
164
|
|
|
$directoryCountryRegions = []; |
165
|
|
|
|
166
|
|
|
// execute the prepared statement |
167
|
|
|
$this->directoryCountryRegionsStatement->execute(); |
168
|
|
|
|
169
|
|
|
// load the available customer groups |
170
|
|
|
$avalaibleDirectoryCountryRegions = $this->directoryCountryRegionsStatement->fetchAll(); |
171
|
|
|
|
172
|
|
|
// fetch the directory regions and assemble them as array with the codes as key |
173
|
|
|
foreach ($avalaibleDirectoryCountryRegions as $countryRegion) { |
174
|
|
|
$directoryCountryRegions[$countryRegion[\TechDivision\Import\Customer\Address\Utils\MemberNames::DIRECTORY_REGION_CODE]] = $countryRegion; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// return the customer groups |
178
|
|
|
return $directoryCountryRegions; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|