1 <?php
2 /* --------------------------------------------------------------
3 CustomerRepository.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('CustomerRepositoryInterface');
13
14 /**
15 * Class CustomerRepository
16 *
17 * This class contains basic methods for finding, creating and deleting customer data
18 *
19 * @category System
20 * @package Customer
21 * @implements CustomerRepositoryInterface
22 */
23 class CustomerRepository implements CustomerRepositoryInterface
24 {
25 /**
26 * Customer writer.
27 * @var CustomerWriterInterface $customerWriter
28 */
29 protected $customerWriter;
30
31 /**
32 * Customer reader.
33 * @var CustomerReaderInterface $customerReader
34 */
35 protected $customerReader;
36
37 /**
38 * Customer deleter.
39 * @var CustomerDeleterInterface $customerDeleter
40 */
41 protected $customerDeleter;
42
43 /**
44 * Customer address repository.
45 * @var CustomerAddressRepositoryInterface $customerAddressRepository
46 */
47 protected $customerAddressRepository;
48
49 /**
50 * Customer factory.
51 * @var AbstractCustomerFactory $customerFactory
52 */
53 protected $customerFactory;
54
55
56 /**
57 * Constructor of the class CustomerRepository.
58 *
59 * @param CustomerWriterInterface $customerWriter Customer writer.
60 * @param CustomerReaderInterface $customerReader Customer reader.
61 * @param CustomerDeleterInterface $customerDeleter Customer deleter.
62 * @param CustomerAddressRepositoryInterface $customerAddressRepository Customer address repository.
63 * @param AbstractCustomerFactory $customerFactory Customer factory.
64 */
65 public function __construct(CustomerWriterInterface $customerWriter,
66 CustomerReaderInterface $customerReader,
67 CustomerDeleterInterface $customerDeleter,
68 CustomerAddressRepositoryInterface $customerAddressRepository,
69 AbstractCustomerFactory $customerFactory)
70 {
71 $this->customerWriter = $customerWriter;
72 $this->customerReader = $customerReader;
73 $this->customerDeleter = $customerDeleter;
74 $this->customerAddressRepository = $customerAddressRepository;
75 $this->customerFactory = $customerFactory;
76 }
77
78
79 /**
80 * Creates a new customer.
81 *
82 * @return Customer Newly created customer.
83 */
84 public function getNewCustomer()
85 {
86 /* @var Customer $customer */
87 $customer = $this->customerFactory->createCustomer();
88
89 $emptyAddress = $this->customerFactory->createCustomerAddress();
90 $this->customerAddressRepository->store($emptyAddress);
91
92 $customer->setDefaultAddress($emptyAddress);
93 $this->store($customer);
94
95 $emptyAddress->setCustomerId(new IdType($customer->getId()));
96 $this->customerAddressRepository->store($emptyAddress);
97
98 return $customer;
99 }
100
101
102 /**
103 * Stores customer data in the database.
104 *
105 * @param CustomerInterface $customer Customer.
106 */
107 public function store(CustomerInterface $customer)
108 {
109 $this->customerWriter->write($customer);
110 }
111
112
113 /**
114 * Finds customer data by an ID.
115 *
116 * @param IdType $customerId Customer's ID.
117 *
118 * @throws InvalidArgumentException If customer has been not found.
119 * @return CustomerInterface
120 */
121 public function getCustomerById(IdType $customerId)
122 {
123 $customer = $this->customerReader->findById($customerId);
124 if($customer == null)
125 {
126 throw new InvalidArgumentException('No customer found by given id');
127 }
128
129 return $customer;
130 }
131
132
133 /**
134 * Finds a registered customer based on the e-mail address.
135 *
136 * @param CustomerEmailInterface $email Customer's E-Mail address.
137 *
138 * @return Customer|null Customer or null if not found.
139 */
140 public function getRegistreeByEmail(CustomerEmailInterface $email)
141 {
142 $customer = $this->customerReader->findRegistreeByEmail($email);
143
144 return $customer;
145 }
146
147
148 /**
149 * Deletes the customer by the ID.
150 *
151 * @param IdType $customerId Customer's ID.
152 */
153 public function deleteCustomerById(IdType $customerId)
154 {
155 $customer = MainFactory::create('Customer');
156 $customer->setId($customerId);
157 $this->customerDeleter->delete($customer);
158 }
159
160
161 /**
162 * Deletes a guest account by its email address.
163 *
164 * @param CustomerEmailInterface $email Guest customer's E-Mail address.
165 */
166 public function deleteGuestByEmail(CustomerEmailInterface $email)
167 {
168 $customer = $this->customerReader->findGuestByEmail($email);
169 if($customer != null)
170 {
171 $this->customerAddressRepository->deleteCustomerAddressesByCustomer($customer);
172 $this->customerDeleter->delete($customer);
173 }
174 }
175
176
177 /**
178 * Returns a guest account by its email address.
179 *
180 * @param CustomerEmailInterface $email Guest customer's E-Mail address.
181 *
182 * @return Customer|null Customer or null if not found.
183 */
184 public function getGuestByEmail(CustomerEmailInterface $email)
185 {
186 $customer = $this->customerReader->findGuestByEmail($email);
187
188 return $customer;
189 }
190
191
192 /**
193 * Filters customer records and returns an array with results.
194 *
195 * Example:
196 * $repository->filterCustomers('customers_id' => 1);
197 *
198 * @param array $conditions Associative array containing the desired field and value.
199 * @param int $limit Result limit
200 * @param int $offset Result offset
201 *
202 * @return array Returns an array that contains customer objects.
203 */
204 public function filterCustomers(array $conditions = array(), $limit = null, $offset = null)
205 {
206 return $this->customerReader->filterCustomers($conditions, $limit, $offset);
207 }
208 }