1 <?php
2 /* --------------------------------------------------------------
3 CustomerCountryZoneRepository.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('CustomerCountryZoneRepositoryInterface');
13
14 /**
15 * Class CustomerCountryZoneRepository
16 *
17 * This class provides basic methods for finding customer country zone data
18 *
19 * @category System
20 * @package Customer
21 * @subpackage CountryZone
22 * @implements CustomerCountryZoneRepositoryInterface
23 */
24 class CustomerCountryZoneRepository implements CustomerCountryZoneRepositoryInterface
25 {
26 /**
27 * @var CustomerCountryZoneReaderInterface
28 */
29 protected $customerCountryZoneReader;
30
31 /**
32 * @var AbstractCustomerFactory
33 */
34 protected $customerFactory;
35
36
37 /**
38 * Constructor of the class CustomerCountryZoneRepository
39 *
40 * @param CustomerCountryZoneReaderInterface $customerCountryZoneReader
41 * @param AbstractCustomerFactory $customerFactory
42 */
43 public function __construct(CustomerCountryZoneReaderInterface $customerCountryZoneReader,
44 AbstractCustomerFactory $customerFactory)
45 {
46 $this->customerCountryZoneReader = $customerCountryZoneReader;
47 $this->customerFactory = $customerFactory;
48 }
49
50
51 /**
52 * @param IdType $countryZoneId
53 *
54 * @throws Exception if country zone not found
55 *
56 * @return CustomerCountryZoneInterface
57 */
58 public function getById(IdType $countryZoneId)
59 {
60 $countryZone = $this->customerCountryZoneReader->findById($countryZoneId);
61
62 if($countryZone === null)
63 {
64 throw new Exception('country zone not found');
65 }
66
67 return $countryZone;
68 }
69
70
71 /**
72 * @param CustomerCountryZoneNameInterface $countryZoneName
73 * @param CustomerCountryInterface $country
74 *
75 * @throws Exception if country zone not found
76 *
77 * @return CustomerCountryZoneInterface
78 */
79 public function getByNameAndCountry(CustomerCountryZoneNameInterface $countryZoneName,
80 CustomerCountryInterface $country)
81 {
82 $countryZone = $this->customerCountryZoneReader->findByNameAndCountry($countryZoneName, $country);
83
84 if($countryZone === null)
85 {
86 throw new Exception('country zone not found');
87 }
88
89 return $countryZone;
90 }
91
92
93 /**
94 * This method will return a new CustomerCountryZone object representing an unknown country zone.
95 * ID is 0 and ISO code is empty.
96 *
97 * @param CustomerCountryZoneNameInterface $countryZoneName
98 *
99 * @return CustomerCountryZone
100 */
101 public function getUnknownCountryZoneByName(CustomerCountryZoneNameInterface $countryZoneName)
102 {
103 return $this->customerFactory->createCustomerCountryZone(new IdType(0),
104 $countryZoneName,
105 MainFactory::create('CustomerCountryZoneIsoCode', ''));
106 }
107
108
109 /**
110 * This method will get the country zone by its name and country if it exists, if not it will return null.
111 *
112 * @param CustomerCountryZoneNameInterface $countryZoneName
113 * @param CustomerCountryInterface $country
114 *
115 * @return CustomerCountryZone|null
116 */
117 public function findByNameAndCountry(CustomerCountryZoneNameInterface $countryZoneName,
118 CustomerCountryInterface $country)
119 {
120 $countryZone = $this->customerCountryZoneReader->findByNameAndCountry($countryZoneName, $country);
121
122 return $countryZone;
123 }
124
125
126 /**
127 * This method will get the country zone by its ID if it exists, if not it will return null.
128 *
129 * @param IdType $countryZoneId
130 *
131 * @return CustomerCountryZone|null
132 */
133 public function findById(IdType $countryZoneId)
134 {
135 $countryZone = $this->customerCountryZoneReader->findById($countryZoneId);
136
137 return $countryZone;
138 }
139
140
141 /**
142 * This method will return an array of country zones found by the country ID.
143 *
144 * @param IdType $countryId
145 *
146 * @return array
147 */
148 public function findCountryZonesByCountryId(IdType $countryId)
149 {
150 $countryZones = $this->customerCountryZoneReader->findCountryZonesByCountryId($countryId);
151 return $countryZones;
152 }
153 }