|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Shop\Addresses\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Shop\Addresses\Address; |
|
6
|
|
|
use App\Shop\Addresses\Exceptions\CreateAddressErrorException; |
|
7
|
|
|
use App\Shop\Addresses\Exceptions\AddressNotFoundException; |
|
8
|
|
|
use App\Shop\Addresses\Repositories\Interfaces\AddressRepositoryInterface; |
|
9
|
|
|
use App\Shop\Addresses\Transformations\AddressTransformable; |
|
10
|
|
|
use App\Shop\Cities\City; |
|
11
|
|
|
use App\Shop\Countries\Country; |
|
12
|
|
|
use App\Shop\Customers\Customer; |
|
13
|
|
|
use App\Shop\Provinces\Province; |
|
14
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
15
|
|
|
use Illuminate\Database\QueryException; |
|
16
|
|
|
use Illuminate\Support\Collection; |
|
17
|
|
|
use Jsdecena\Baserepo\BaseRepository; |
|
18
|
|
|
|
|
19
|
|
|
class AddressRepository extends BaseRepository implements AddressRepositoryInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use AddressTransformable; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* AddressRepository constructor. |
|
25
|
|
|
* @param Address $address |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Address $address) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($address); |
|
30
|
|
|
$this->model = $address; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create the address |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $data |
|
37
|
|
|
* |
|
38
|
|
|
* @return Address |
|
39
|
|
|
* @throws CreateAddressErrorException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function createAddress(array $data) : Address |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
|
|
return $this->create($data); |
|
|
|
|
|
|
45
|
|
|
} catch (QueryException $e) { |
|
46
|
|
|
throw new CreateAddressErrorException('Address creation error'); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Attach the customer to the address |
|
52
|
|
|
* |
|
53
|
|
|
* @param Address $address |
|
54
|
|
|
* @param Customer $customer |
|
55
|
|
|
*/ |
|
56
|
|
|
public function attachToCustomer(Address $address, Customer $customer) |
|
57
|
|
|
{ |
|
58
|
|
|
$customer->addresses()->save($address); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param array $data |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public function updateAddress(array $data): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->update($data); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Soft delete the address |
|
72
|
|
|
* |
|
73
|
|
|
*/ |
|
74
|
|
|
public function deleteAddress() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->model->customer()->dissociate(); |
|
77
|
|
|
return $this->model->delete(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* List all the address |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $order |
|
84
|
|
|
* @param string $sort |
|
85
|
|
|
* @param array $columns |
|
86
|
|
|
* @return array|Collection |
|
87
|
|
|
*/ |
|
88
|
|
|
public function listAddress(string $order = 'id', string $sort = 'desc', array $columns = ['*']) : Collection |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->all($columns, $order, $sort); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Return the address |
|
95
|
|
|
* |
|
96
|
|
|
* @param int $id |
|
97
|
|
|
* |
|
98
|
|
|
* @return Address |
|
99
|
|
|
* @throws AddressNotFoundException |
|
100
|
|
|
*/ |
|
101
|
|
|
public function findAddressById(int $id) : Address |
|
102
|
|
|
{ |
|
103
|
|
|
try { |
|
104
|
|
|
return $this->findOneOrFail($id); |
|
|
|
|
|
|
105
|
|
|
} catch (ModelNotFoundException $e) { |
|
106
|
|
|
throw new AddressNotFoundException('Address not found.'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Return the address |
|
112
|
|
|
* |
|
113
|
|
|
* @param int $id |
|
114
|
|
|
* |
|
115
|
|
|
* @return Address |
|
116
|
|
|
* @throws AddressNotFoundException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function findCustomerAddressById(int $id, Customer $customer) : Address |
|
119
|
|
|
{ |
|
120
|
|
|
try |
|
121
|
|
|
{ |
|
122
|
|
|
return $customer |
|
123
|
|
|
->addresses() |
|
124
|
|
|
->whereId($id) |
|
125
|
|
|
->firstOrFail(); |
|
126
|
|
|
} |
|
127
|
|
|
catch (ModelNotFoundException $e) |
|
128
|
|
|
{ |
|
129
|
|
|
throw new AddressNotFoundException('Address not found.'); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Return the customer owner of the address |
|
135
|
|
|
* |
|
136
|
|
|
* @return Customer |
|
137
|
|
|
*/ |
|
138
|
|
|
public function findCustomer() : Customer |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->model->customer; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $text |
|
145
|
|
|
* @return mixed |
|
146
|
|
|
*/ |
|
147
|
|
|
public function searchAddress(string $text = null) : Collection |
|
148
|
|
|
{ |
|
149
|
|
|
if (is_null($text)) { |
|
150
|
|
|
return $this->all(['*'], 'address_1', 'asc'); |
|
151
|
|
|
} |
|
152
|
|
|
return $this->model->searchAddress($text)->get(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return Country |
|
157
|
|
|
*/ |
|
158
|
|
|
public function findCountry() : Country |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->model->country; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return Province |
|
165
|
|
|
*/ |
|
166
|
|
|
public function findProvince() : Province |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->model->province; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function findCity() : City |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->model->city; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return Collection |
|
178
|
|
|
*/ |
|
179
|
|
|
public function findOrders() : Collection |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->model->orders()->get(); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|