|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Shop\Customers\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Shop\Addresses\Address; |
|
6
|
|
|
use App\Shop\Base\BaseRepository; |
|
7
|
|
|
use App\Shop\Customers\Customer; |
|
8
|
|
|
use App\Shop\Customers\Exceptions\CreateCustomerInvalidArgumentException; |
|
9
|
|
|
use App\Shop\Customers\Exceptions\CustomerNotFoundException; |
|
10
|
|
|
use App\Shop\Customers\Exceptions\CustomerPaymentChargingErrorException; |
|
11
|
|
|
use App\Shop\Customers\Exceptions\UpdateCustomerInvalidArgumentException; |
|
12
|
|
|
use App\Shop\Customers\Repositories\Interfaces\CustomerRepositoryInterface; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
14
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
15
|
|
|
use Illuminate\Database\QueryException; |
|
16
|
|
|
use Illuminate\Support\Collection as Support; |
|
17
|
|
|
|
|
18
|
|
|
class CustomerRepository extends BaseRepository implements CustomerRepositoryInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* CustomerRepository constructor. |
|
22
|
|
|
* @param Customer $customer |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(Customer $customer) |
|
25
|
|
|
{ |
|
26
|
|
|
parent::__construct($customer); |
|
27
|
|
|
$this->model = $customer; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* List all the employees |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $order |
|
34
|
|
|
* @param string $sort |
|
35
|
|
|
* @param array $columns |
|
36
|
|
|
* @return \Illuminate\Support\Collection |
|
37
|
|
|
*/ |
|
38
|
|
|
public function listCustomers(string $order = 'id', string $sort = 'desc', array $columns = ['*']) : Support |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->all($columns, $order, $sort); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create the customer |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $params |
|
47
|
|
|
* @return Customer |
|
48
|
|
|
* @throws CreateCustomerInvalidArgumentException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function createCustomer(array $params) : Customer |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
|
|
$data = collect($params)->except('password')->all(); |
|
54
|
|
|
|
|
55
|
|
|
$customer = new Customer($data); |
|
56
|
|
|
if (isset($params['password'])) { |
|
57
|
|
|
$customer->password = bcrypt($params['password']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$customer->save(); |
|
61
|
|
|
|
|
62
|
|
|
return $customer; |
|
63
|
|
|
} catch (QueryException $e) { |
|
64
|
|
|
throw new CreateCustomerInvalidArgumentException($e->getMessage(), 500, $e); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Update the customer |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $params |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
public function updateCustomer(array $params) : bool |
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
|
|
return $this->model->update($params); |
|
78
|
|
|
} catch (QueryException $e) { |
|
79
|
|
|
throw new UpdateCustomerInvalidArgumentException('Cannot update customer', 500, $e); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Find the customer or fail |
|
85
|
|
|
* |
|
86
|
|
|
* @param int $id |
|
87
|
|
|
* @return Customer |
|
88
|
|
|
*/ |
|
89
|
|
|
public function findCustomerById(int $id) : Customer |
|
90
|
|
|
{ |
|
91
|
|
|
try { |
|
92
|
|
|
return $this->findOneOrFail($id); |
|
|
|
|
|
|
93
|
|
|
} catch (ModelNotFoundException $e) { |
|
94
|
|
|
throw new CustomerNotFoundException('Cannot find customer', $e); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Delete a customer |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function deleteCustomer() : bool |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->model->delete(); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param Address $address |
|
110
|
|
|
* @return Address |
|
111
|
|
|
*/ |
|
112
|
|
|
public function attachAddress(Address $address) : Address |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->model->addresses()->save($address); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Find the address attached to the customer |
|
119
|
|
|
* |
|
120
|
|
|
* @return mixed |
|
121
|
|
|
*/ |
|
122
|
|
|
public function findAddresses() : Support |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->model->addresses; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return Collection |
|
129
|
|
|
*/ |
|
130
|
|
|
public function findOrders() : Collection |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->model->orders()->get(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $text |
|
137
|
|
|
* @return mixed |
|
138
|
|
|
*/ |
|
139
|
|
|
public function searchCustomer(string $text) : Collection |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->model->search($text, ['name' => 10, 'email' => 5])->get(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param int $amount |
|
146
|
|
|
* @param array $options |
|
147
|
|
|
* @return \Stripe\Charge |
|
148
|
|
|
* @throws CustomerPaymentChargingErrorException |
|
149
|
|
|
*/ |
|
150
|
|
|
public function charge(int $amount, array $options) |
|
151
|
|
|
{ |
|
152
|
|
|
try { |
|
153
|
|
|
return $this->model->charge($amount * 100, $options); |
|
|
|
|
|
|
154
|
|
|
} catch (\Exception $e) { |
|
155
|
|
|
throw new CustomerPaymentChargingErrorException($e); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|