1 <?php
2 /* --------------------------------------------------------------
3 CountryService.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('CountryServiceInterface');
13
14 /**
15 * Class CountryService
16 *
17 * This class is used for finding country data
18 *
19 * @category System
20 * @package Customer
21 * @subpackage Country
22 * @implements CountryServiceInterface
23 */
24 class CountryService implements CountryServiceInterface
25 {
26 /**
27 * @var CustomerCountryRepositoryInterface
28 */
29 protected $customerCountryRepo;
30 /**
31 * @var CustomerCountryZoneRepositoryInterface
32 */
33 protected $customerCountryZoneRepo;
34
35
36 /**
37 * Constructor of the class CountryService
38 *
39 * @param CustomerCountryRepositoryInterface $customerCountryRepo
40 * @param CustomerCountryZoneRepositoryInterface $customerCountryZoneRepo
41 */
42 public function __construct(CustomerCountryRepositoryInterface $customerCountryRepo,
43 CustomerCountryZoneRepositoryInterface $customerCountryZoneRepo)
44 {
45 $this->customerCountryRepo = $customerCountryRepo;
46 $this->customerCountryZoneRepo = $customerCountryZoneRepo;
47 }
48
49
50 /**
51 * Getter method for the country ID
52 *
53 * @param IdType $id
54 *
55 * @return CustomerCountryInterface
56 */
57 public function getCountryById(IdType $id)
58 {
59 return $this->customerCountryRepo->getById($id);
60 }
61
62
63 /**
64 * Getter method for the country zone
65 *
66 * @param $p_zoneName
67 * @param CustomerCountryInterface $customerCountry
68 *
69 * @return CustomerCountryZoneInterface
70 */
71 public function getCountryZoneByNameAndCountry($p_zoneName, CustomerCountryInterface $customerCountry)
72 {
73 if(is_a($p_zoneName, 'CustomerCountryZoneNameInterface'))
74 {
75 $zoneName = $p_zoneName;
76 }
77 else
78 {
79 $zoneName = MainFactory::create('CustomerCountryZoneName', $p_zoneName);
80 }
81
82 return $this->customerCountryZoneRepo->getByNameAndCountry($zoneName, $customerCountry);
83 }
84
85
86 /**
87 * @param IdType $id
88 *
89 * @return CustomerCountryZoneInterface
90 */
91 public function getCountryZoneById(IdType $id)
92 {
93 return $this->customerCountryZoneRepo->getById($id);
94 }
95
96
97 /**
98 * This method will return a new CustomerCountryZone object representing an unknown country zone.
99 *
100 * @param string $p_zoneName
101 *
102 * @return CustomerCountryZone
103 */
104 public function getUnknownCountryZoneByName($p_zoneName)
105 {
106 $countryZoneName = MainFactory::create('CustomerCountryZoneName', $p_zoneName);
107
108 return $this->customerCountryZoneRepo->getUnknownCountryZoneByName($countryZoneName);
109 }
110
111
112 /**
113 * This method will return an array of CustomerCountryZone objects found by the country ID. If the country has
114 * no zones, an empty array will be returned
115 *
116 * @param IdType $countryId
117 *
118 * @return array of CustomerCountryZone objects
119 */
120 public function findCountryZonesByCountryId(IdType $countryId)
121 {
122 $countryZones = $this->customerCountryZoneRepo->findCountryZonesByCountryId($countryId);
123
124 return $countryZones;
125 }
126
127
128 /**
129 * Checks if there is a country zone in a country
130 *
131 * @param CustomerCountryZoneInterface $customerCountryZone
132 * @param CustomerCountryInterface $customerCountry
133 *
134 * @return bool true|false if country zone exists|not exists
135 */
136 public function countryZoneExistsInCountry(CustomerCountryZoneInterface $customerCountryZone,
137 CustomerCountryInterface $customerCountry)
138 {
139 $countryZones = $this->customerCountryZoneRepo->findCountryZonesByCountryId(new IdType((int)$customerCountry->getId()));
140 $countryZoneExistsInCountry = in_array($customerCountryZone, $countryZones);
141
142 return $countryZoneExistsInCountry;
143 }
144
145
146 /**
147 * Checks if country has country zones
148 *
149 * @param CustomerCountryInterface $customerCountry
150 *
151 * @return bool true|false if there are|are not country zones
152 */
153 public function countryHasCountryZones(CustomerCountryInterface $customerCountry)
154 {
155 $countryZones = $this->customerCountryZoneRepo->findCountryZonesByCountryId(new IdType($customerCountry->getId()));
156 $countryHasCountryZones = !empty($countryZones);
157
158 return $countryHasCountryZones;
159 }
160
161
162 /**
163 * Get customer by country name.
164 *
165 * @param string $p_countryName
166 *
167 * @return CustomerCountry
168 */
169 public function getCountryByName($p_countryName)
170 {
171 $countryName = MainFactory::create('CustomerCountryName', $p_countryName);
172
173 return $this->customerCountryRepo->getByName($countryName);
174 }
175 }
176