1 <?php
2 3 4 5 6 7 8 9 10
11
12 MainFactory::load_class('CustomerReaderInterface');
13
14 15 16 17 18 19 20 21 22
23 class CustomerReader implements CustomerReaderInterface
24 {
25 26 27 28
29 protected $db;
30
31 32 33 34
35 protected $customerFactory;
36
37 38 39 40
41 protected $customerAddressRepository;
42
43 44 45 46
47 protected $stringHelper;
48
49 50 51 52
53 protected $isGuest;
54
55
56 57 58 59 60 61 62 63 64 65
66 public function __construct(AbstractCustomerFactory $customerFactory,
67 CustomerAddressRepositoryInterface $customerAddressRepository,
68 CI_DB_query_builder $dbQueryBuilder)
69 {
70 $this->customerFactory = $customerFactory;
71 $this->customerAddressRepository = $customerAddressRepository;
72 $this->db = $dbQueryBuilder;
73
74 $this->stringHelperService = StaticCrossCuttingLoader::getObject('StringHelper');
75 }
76
77
78 79 80 81 82 83 84
85 public function findById(IdType $id)
86 {
87 $filterArray = array('customers_id' => (string)$id);
88
89 return $this->_findByFilter($filterArray);
90 }
91
92
93 94 95 96 97 98 99
100 public function findRegistreeByEmail(CustomerEmailInterface $email)
101 {
102 $isGuest = false;
103 $filterArray = array(
104 'customers_email_address' => (string)$email,
105 'account_type' => (string)(int)$isGuest
106 );
107
108 return $this->_findByFilter($filterArray);
109 }
110
111
112 113 114 115 116 117 118
119 public function findGuestByEmail(CustomerEmailInterface $email)
120 {
121 $isGuest = true;
122 $filterArray = array(
123 'customers_email_address' => (string)$email,
124 'account_type' => (string)(int)$isGuest
125 );
126
127 return $this->_findByFilter($filterArray);
128 }
129
130
131 132 133 134 135 136 137
138 protected function _findByFilter(array $filterArray)
139 {
140 $customerDataArray = $this->db->get_where('customers', $filterArray)->row_array();
141 if(empty($customerDataArray))
142 {
143 return null;
144 }
145
146 return $this->_createCustomerByArray($customerDataArray);
147 }
148
149
150 151 152 153 154 155 156 157 158 159 160
161 protected function _createCustomerByArray(array $customerDataArray)
162 {
163 $customerDataArray = $this->stringHelperService->convertNullValuesToStringInArray($customerDataArray);
164
165 $customer = $this->customerFactory->createCustomer();
166 $customer->setId(new IdType($customerDataArray['customers_id']));
167 $customer->setCustomerNumber(MainFactory::create('CustomerNumber', $customerDataArray['customers_cid']));
168 $customer->setVatNumber(MainFactory::create('CustomerVatNumber', $customerDataArray['customers_vat_id']));
169 $customer->setVatNumberStatus($customerDataArray['customers_vat_id_status']);
170 $customer->setStatusId($customerDataArray['customers_status']);
171 $customer->setGender(MainFactory::create('CustomerGender', $customerDataArray['customers_gender']));
172 $customer->setFirstname(MainFactory::create('CustomerFirstname', $customerDataArray['customers_firstname']));
173 $customer->setLastname(MainFactory::create('CustomerLastname', $customerDataArray['customers_lastname']));
174 $customer->setDateOfBirth(MainFactory::create('CustomerDateOfBirth', $customerDataArray['customers_dob']));
175 $customer->setEmail(MainFactory::create('CustomerEmail', $customerDataArray['customers_email_address']));
176 $customer->setPassword(MainFactory::create('CustomerPassword', $customerDataArray['customers_password'], true));
177 $customer->setTelephoneNumber(MainFactory::create('CustomerCallNumber',
178 $customerDataArray['customers_telephone']));
179 $customer->setFaxNumber(MainFactory::create('CustomerCallNumber', $customerDataArray['customers_fax']));
180 $customer->setGuest((boolean)(int)$customerDataArray['account_type']);
181
182 $customerAddress = $this->customerAddressRepository->getById(new IdType((int)$customerDataArray['customers_default_address_id']));
183 $customer->setDefaultAddress($customerAddress);
184
185 return $customer;
186 }
187
188
189 190 191 192 193 194 195 196 197 198 199 200
201 public function filterCustomers(array $conditions = array(), $limit = null, $offset = null)
202 {
203 if($limit !== null)
204 {
205 $this->db->limit((int)$limit, (int)$offset);
206 }
207
208 if(count($conditions) > 1)
209 {
210 foreach($conditions as $field => $value)
211 {
212 $this->db->or_where($field, $value);
213 }
214 $results = $this->db->get('customers')->result_array();
215 }
216 else
217 {
218 $results = $this->db->get_where('customers', $conditions)->result_array();
219 }
220
221 $customers = array();
222
223 foreach($results as $item)
224 {
225 $customers[] = $this->_createCustomerByArray($item);
226 }
227
228 return $customers;
229 }
230 }