1 <?php
2 /* --------------------------------------------------------------
3 AddressBookService.inc.php 2015-07-06 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('AddressBookServiceInterface');
13
14 /**
15 * Class AddressBookService
16 *
17 * This class is used to manage addresses
18 *
19 * @category System
20 * @package Customer
21 * @subpackage Address
22 * @implements AddressBookServiceInterface
23 */
24 class AddressBookService implements AddressBookServiceInterface
25 {
26 /**
27 * @var CustomerAddressRepositoryInterface
28 */
29 protected $customerAddressRepository;
30
31
32 /**
33 * Constructor of the class AddressBookService
34 *
35 * @param CustomerAddressRepositoryInterface $addressRepository
36 */
37 public function __construct(CustomerAddressRepositoryInterface $addressRepository)
38 {
39 $this->customerAddressRepository = $addressRepository;
40 }
41
42
43 /**
44 * Method to add a new address in the address book
45 *
46 * @param AddressBlockInterface $addressBlock
47 * @param CustomerInterface $customer
48 *
49 * @return CustomerAddressInterface
50 */
51 public function createNewAddress(AddressBlockInterface $addressBlock, CustomerInterface $customer)
52 {
53 /* @var CustomerAddress $address */
54 $address = $this->customerAddressRepository->getNewAddress();
55 $address->importAddressBlock($addressBlock);
56 $address->setCustomerId(new IdType($customer->getId()));
57 $this->customerAddressRepository->store($address);
58
59 return $address;
60 }
61
62
63 /**
64 * Method to update an address in the address book
65 *
66 * @param AddressBlockInterface $addressBlock
67 * @param CustomerAddressInterface $address
68 *
69 * @return CustomerAddressInterface
70 */
71 public function updateAddress(AddressBlockInterface $addressBlock, CustomerAddressInterface $address)
72 {
73 $address->importAddressBlock($addressBlock);
74 $this->customerAddressRepository->store($address);
75
76 return $address;
77 }
78
79
80 /**
81 * @param CustomerAddressInterface $address
82 */
83 public function deleteAddress(CustomerAddressInterface $address)
84 {
85 $this->customerAddressRepository->deleteCustomerAddress($address);
86 }
87
88
89 /**
90 * @param IdType $addressId
91 *
92 * @return CustomerAddress
93 */
94 public function findAddressById(IdType $addressId)
95 {
96 return $this->customerAddressRepository->findById($addressId);
97 }
98
99
100 /**
101 * @param CustomerAddressInterface $customerAddress
102 */
103 public function updateCustomerAddress(CustomerAddressInterface $customerAddress)
104 {
105 $this->customerAddressRepository->store($customerAddress);
106 }
107
108
109 /**
110 * Get customer addresses.
111 *
112 * @param CustomerInterface $customer Contains the customer data.
113 *
114 * @return array Returns an array of CustomerAddress objects.
115 */
116 public function getCustomerAddresses(CustomerInterface $customer)
117 {
118 return $this->customerAddressRepository->getCustomerAddresses($customer);
119 }
120
121
122 /**
123 * Get all registered addresses.
124 *
125 * @return array Returns an array of CustomerAddress objects.
126 */
127 public function getAllAddresses()
128 {
129 return $this->customerAddressRepository->getAllAddresses();
130 }
131
132
133 /**
134 * Filter registered addresses by string.
135 *
136 * @param string $p_keyword Used to filter the address records.
137 *
138 * @return array Returns an array of CustomerAddress objects.
139 */
140 public function filterAddresses($p_keyword)
141 {
142 return $this->customerAddressRepository->filterAddresses($p_keyword);
143 }
144 }