1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\Fastbill\Api\Service\Customer; |
4
|
|
|
|
5
|
|
|
use Speicher210\Fastbill\Api\AbstractService; |
6
|
|
|
use Speicher210\Fastbill\Api\Model\Customer; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Service for customers. |
10
|
|
|
*/ |
11
|
|
|
class CustomerService extends AbstractService |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Get the customers. |
15
|
|
|
* |
16
|
|
|
* @param Get\RequestData|null $requestData The request data. |
17
|
|
|
* @return Get\ApiResponse |
18
|
|
|
*/ |
19
|
6 |
|
public function getCustomers(Get\RequestData $requestData = null) |
20
|
|
|
{ |
21
|
6 |
|
$request = new Get\Request($requestData); |
22
|
|
|
|
23
|
6 |
|
$apiResponse = $this->sendRequest($request, Get\ApiResponse::class); |
24
|
|
|
|
25
|
|
|
/** @var Get\Response $response */ |
26
|
6 |
|
$response = $apiResponse->getResponse(); |
27
|
6 |
|
foreach ($response->getCustomers() as $customer) { |
28
|
6 |
|
if ($customer->getBirthday() instanceof \DateTime) { |
29
|
3 |
|
$customer->getBirthday()->setTime(0, 0, 0); |
30
|
3 |
|
} |
31
|
6 |
|
} |
32
|
|
|
|
33
|
6 |
|
return $apiResponse; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get one customer by ID. |
38
|
|
|
* |
39
|
|
|
* @param integer $customerId The customer ID. |
40
|
|
|
* @return Customer|null |
41
|
|
|
*/ |
42
|
3 |
View Code Duplication |
public function getCustomer($customerId) |
|
|
|
|
43
|
|
|
{ |
44
|
3 |
|
$requestData = new Get\RequestData(); |
45
|
|
|
$requestData->setCustomerId($customerId); |
46
|
3 |
|
|
47
|
|
|
$customers = $this->getCustomers($requestData)->getResponse()->getCustomers(); |
48
|
|
|
|
49
|
|
|
return isset($customers[0]) ? $customers[0] : null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get once customer by the external ID. |
54
|
|
|
* |
55
|
3 |
|
* @param string $customerExternalUid The external customer ID. |
56
|
|
|
* @return Customer|null |
57
|
3 |
|
*/ |
58
|
|
View Code Duplication |
public function getCustomerByExternalUid($customerExternalUid) |
|
|
|
|
59
|
3 |
|
{ |
60
|
|
|
$requestData = new Get\RequestData(); |
61
|
|
|
$requestData->setCustomerExternalUid($customerExternalUid); |
62
|
|
|
|
63
|
|
|
$customers = $this->getCustomers($requestData)->getResponse()->getCustomers(); |
64
|
|
|
|
65
|
|
|
return isset($customers[0]) ? $customers[0] : null; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
/** |
69
|
|
|
* Update a customer. |
70
|
3 |
|
* |
71
|
3 |
|
* @param Update\RequestData $requestData The data to send. |
72
|
|
|
* @return Update\ApiResponse |
73
|
3 |
|
*/ |
74
|
|
|
public function updateCustomer(Update\RequestData $requestData) |
75
|
|
|
{ |
76
|
|
|
$request = new Update\Request($requestData); |
77
|
|
|
|
78
|
|
|
return $this->sendRequest($request, Update\ApiResponse::class); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a customer. |
83
|
3 |
|
* |
84
|
|
|
* @param Customer $customer The customer data to send. |
85
|
3 |
|
* @return Create\ApiResponse |
86
|
3 |
|
*/ |
87
|
|
|
public function createCustomer(Customer $customer) |
88
|
3 |
|
{ |
89
|
|
|
$request = new Create\Request($customer); |
90
|
|
|
|
91
|
|
|
return $this->sendRequest($request, Create\ApiResponse::class); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Delete a customer. |
96
|
|
|
* |
97
|
3 |
|
* @param integer $customerId The customer ID to delete. |
98
|
|
|
* @return Delete\ApiResponse |
99
|
3 |
|
*/ |
100
|
3 |
|
public function deleteCustomer($customerId) |
101
|
|
|
{ |
102
|
3 |
|
$requestData = new Delete\RequestData($customerId); |
103
|
|
|
$request = new Delete\Request($requestData); |
104
|
|
|
|
105
|
|
|
return $this->sendRequest($request, Delete\ApiResponse::class); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Add credits to a customer. |
110
|
|
|
* |
111
|
|
|
* @param integer $customerId The customer ID. |
112
|
|
|
* @param float $amount The amount of credit to add. |
113
|
|
|
* @return AddCredits\ApiResponse |
114
|
|
|
*/ |
115
|
|
|
public function addCredits($customerId, $amount) |
116
|
|
|
{ |
117
|
|
|
$requestData = new AddCredits\RequestData($customerId, $amount); |
118
|
|
|
$request = new AddCredits\Request($requestData); |
119
|
|
|
|
120
|
|
|
return $this->sendRequest($request, AddCredits\ApiResponse::class); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Add credits to a customer. |
125
|
|
|
* |
126
|
|
|
* @param integer $customerId The customer ID. |
127
|
|
|
* @return CreateSecureLink\ApiResponse |
128
|
|
|
*/ |
129
|
|
|
public function createSecureLink($customerId) |
130
|
|
|
{ |
131
|
|
|
$requestData = new CreateSecureLink\RequestData($customerId); |
132
|
|
|
$request = new CreateSecureLink\Request($requestData); |
133
|
|
|
|
134
|
|
|
return $this->sendRequest($request, CreateSecureLink\ApiResponse::class); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.