1 <?php
2 /* --------------------------------------------------------------
3 CustomerCountryReader.inc.php 2015-02-18 gm
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('CustomerCountryReaderInterface');
13
14 /**
15 * Class CustomerCountryReader
16 *
17 * This class is used for reading customer country data from the database
18 *
19 * @category System
20 * @package Customer
21 * @subpackage Country
22 * @implements CustomerCountryReaderInterface
23 */
24 class CustomerCountryReader implements CustomerCountryReaderInterface
25 {
26 /**
27 * @var AbstractCustomerFactory
28 */
29 protected $customerFactory;
30 /**
31 * @var CI_DB_query_builder
32 */
33 protected $db;
34
35
36 /**
37 * Constructor of the class CustomerCountryReader
38 *
39 * @param AbstractCustomerFactory $customerFactory
40 * @param CI_DB_query_builder $dbQueryBuilder
41 */
42 public function __construct(AbstractCustomerFactory $customerFactory, CI_DB_query_builder $dbQueryBuilder)
43 {
44 $this->customerFactory = $customerFactory;
45 $this->db = $dbQueryBuilder;
46 }
47
48
49 /**
50 * @param IdType $countryId
51 *
52 * @return CustomerCountry|null
53 */
54 public function findById(IdType $countryId)
55 {
56 $countryDataArray = $this->db->get_where('countries', array('countries_id' => $countryId->asInt()))
57 ->row_array();
58 if(empty($countryDataArray))
59 {
60 return null;
61 }
62
63 return $this->_getCountryByArray($countryDataArray);
64 }
65
66
67 /**
68 * @param $countryName
69 *
70 * @return CustomerCountry|null
71 */
72 public function findByName(CustomerCountryNameInterface $countryName)
73 {
74 $countryDataArray = $this->db->get_where('countries', array('countries_name' => (string)$countryName))
75 ->row_array();
76
77 if(empty($countryDataArray))
78 {
79 return null;
80 }
81
82 return $this->_getCountryByArray($countryDataArray);
83 }
84
85
86 /**
87 * @param $countryDataArray
88 *
89 * @return CustomerCountry
90 */
91 protected function _getCountryByArray($countryDataArray)
92 {
93 $country = $this->customerFactory->createCustomerCountry(new IdType((int)$countryDataArray['countries_id']),
94 MainFactory::create('CustomerCountryName',
95 $countryDataArray['countries_name']),
96 MainFactory::create('CustomerCountryIso2',
97 $countryDataArray['countries_iso_code_2']),
98 MainFactory::create('CustomerCountryIso3',
99 $countryDataArray['countries_iso_code_3']),
100 new IdType((int)$countryDataArray['address_format_id']),
101 (boolean)(int)$countryDataArray['status']);
102
103 return $country;
104 }
105 }