1 <?php
2 3 4 5 6 7 8 9 10
11
12 MainFactory::load_class('CustomerAddressReaderInterface');
13
14 15 16 17 18 19 20 21 22 23
24 class CustomerAddressReader implements CustomerAddressReaderInterface
25 {
26 27 28
29 protected $db;
30 31 32
33 protected $customerFactory;
34 35 36
37 protected $countryService;
38 39 40
41 protected $stringHelper;
42
43
44 45 46 47 48 49 50 51 52 53
54 public function __construct(AbstractCustomerFactory $customerFactory, CountryServiceInterface $countryService,
55 CI_DB_query_builder $dbQueryBuilder)
56 {
57 $this->customerFactory = $customerFactory;
58 $this->countryService = $countryService;
59 $this->db = $dbQueryBuilder;
60
61 $this->stringHelper = StaticCrossCuttingLoader::getObject('StringHelper');
62 }
63
64
65 66 67 68 69 70
71 public function getById(IdType $id)
72 {
73 $address = $this->findById($id);
74 if($address === null)
75 {
76 throw new InvalidArgumentException('No Address found for the given ID.');
77 }
78
79 return $address;
80 }
81
82
83 84 85 86
87 public function findById(IdType $id)
88 {
89 $addressDataResult = $this->db->get_where('address_book', array('address_book_id' => (int)(string)$id));
90 $addressDataArray = $addressDataResult->row_array();
91 if($addressDataResult->num_rows() == 0)
92 {
93 return null;
94 }
95
96 return $this->_createCustomerAddressByArray($addressDataArray);
97 }
98
99
100 101 102 103 104 105 106
107 public function findAddressesByCustomer(CustomerInterface $customer)
108 {
109 $addressesArray = $this->db->get_where('address_book', array('customers_id' => $customer->getId()))
110 ->result_array();
111
112 foreach($addressesArray as &$address)
113 {
114 $address = $this->_createCustomerAddressByArray($address);
115 }
116
117 return $addressesArray;
118 }
119
120
121 122 123
124 public function getAllAddresses()
125 {
126 $addressesArray = $this->db->get('address_book')->result_array();
127
128 foreach($addressesArray as &$address)
129 {
130 $address = $this->_createCustomerAddressByArray($address);
131 }
132
133 return $addressesArray;
134 }
135
136
137 138 139 140 141 142 143 144 145
146 public function filterAddresses($p_keyword)
147 {
148
149
150 $this->db->like('entry_gender', $p_keyword);
151 $this->db->or_like('entry_company', $p_keyword);
152 $this->db->or_like('entry_firstname', $p_keyword);
153 $this->db->or_like('entry_lastname', $p_keyword);
154 $this->db->or_like('entry_street_address', $p_keyword);
155 $this->db->or_like('entry_suburb', $p_keyword);
156 $this->db->or_like('entry_postcode', $p_keyword);
157 $this->db->or_like('entry_city', $p_keyword);
158 $this->db->or_like('entry_state', $p_keyword);
159
160 $addressesArray = $this->db->get('address_book')->result_array();
161
162 foreach($addressesArray as &$address)
163 {
164 $address = $this->_createCustomerAddressByArray($address);
165 }
166
167 return $addressesArray;
168 }
169
170 171 172 173 174
175 protected function _createCustomerAddressByArray(array $addressDataArray)
176 {
177 $addressDataArray = $this->stringHelper->convertNullValuesToStringInArray($addressDataArray);
178
179 $customerAddress = $this->customerFactory->createCustomerAddress();
180 $customerAddress->setId(new IdType((int)$addressDataArray['address_book_id']));
181 $customerAddress->setCustomerId(new IdType($addressDataArray['customers_id']));
182 $customerAddress->setGender(MainFactory::create('CustomerGender', $addressDataArray['entry_gender']));
183 $customerAddress->setCompany(MainFactory::create('CustomerCompany', $addressDataArray['entry_company']));
184 $customerAddress->setB2BStatus(MainFactory::create('CustomerB2BStatus', (bool)(int)$addressDataArray['customer_b2b_status']));
185 $customerAddress->setFirstname(MainFactory::create('CustomerFirstname', $addressDataArray['entry_firstname']));
186 $customerAddress->setLastname(MainFactory::create('CustomerLastname', $addressDataArray['entry_lastname']));
187 $customerAddress->setStreet(MainFactory::create('CustomerStreet', $addressDataArray['entry_street_address']));
188 $customerAddress->setSuburb(MainFactory::create('CustomerSuburb', $addressDataArray['entry_suburb']));
189 $customerAddress->setPostcode(MainFactory::create('CustomerPostcode', $addressDataArray['entry_postcode']));
190 $customerAddress->setCity(MainFactory::create('CustomerCity', $addressDataArray['entry_city']));
191
192 $country = $this->countryService->getCountryById(new IdType(
193 $addressDataArray['entry_country_id']));
194 $customerAddress->setCountry($country);
195
196 $state = MainFactory::create('CustomerCountryZoneName', $addressDataArray['entry_state']);
197
198 if($this->countryService->countryHasCountryZones($country) && (string)$state !== '')
199 {
200 $countryZone = $this->countryService->getCountryZoneByNameAndCountry($state, $country);
201 }
202 else
203 {
204 $countryZone = $this->customerFactory->createCustomerCountryZone(new IdType(0),
205 $state,
206 MainFactory::create('CustomerCountryZoneIsoCode', ''));
207 }
208
209 $customerAddress->setCountryZone($countryZone);
210
211 return $customerAddress;
212 }
213 }