1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FAPI\Sylius\Api; |
11
|
|
|
|
12
|
|
|
use FAPI\Sylius\Exception; |
13
|
|
|
use FAPI\Sylius\Exception\InvalidArgumentException; |
14
|
|
|
use FAPI\Sylius\Model\Customer\Customer as Model; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Kasim Taskin <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class Customer extends HttpApi |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @throws Exception |
24
|
|
|
* |
25
|
|
|
* @return Model|ResponseInterface |
26
|
|
|
*/ |
27
|
|
|
public function create(string $email, string $firstName, string $lastName, string $gender, array $optionalParams = []) |
28
|
|
|
{ |
29
|
|
|
$params = $this->validateAndGetParams($email, $firstName, $lastName, $gender, $optionalParams); |
30
|
|
|
|
31
|
|
|
$response = $this->httpPost('/api/v1/customers/', $params); |
32
|
|
|
if (!$this->hydrator) { |
33
|
|
|
return $response; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// Use any valid status code here |
37
|
|
|
if (201 !== $response->getStatusCode()) { |
38
|
|
|
$this->handleErrors($response); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this->hydrator->hydrate($response, Model::class); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws Exception |
46
|
|
|
* |
47
|
|
|
* @return ResponseInterface |
48
|
|
|
*/ |
49
|
|
|
public function update(int $id, string $email, string $firstName, string $lastName, string $gender, array $optionalParams = []) |
50
|
|
|
{ |
51
|
|
|
$params = $this->validateAndGetParams($email, $firstName, $lastName, $gender, $optionalParams); |
52
|
|
|
|
53
|
|
|
$response = $this->httpPut('/api/v1/customers/'.$id, $params); |
54
|
|
|
|
55
|
|
|
// Use any valid status code here |
56
|
|
|
if (204 !== $response->getStatusCode()) { |
57
|
|
|
$this->handleErrors($response); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $response; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $email |
65
|
|
|
* @param string $firstName |
66
|
|
|
* @param string $lastName |
67
|
|
|
* @param string $gender |
68
|
|
|
* @param array $optionalParams |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
private function validateAndGetParams(string $email, string $firstName, string $lastName, string $gender, array $optionalParams): array |
73
|
|
|
{ |
74
|
|
|
if (empty($email)) { |
75
|
|
|
throw new InvalidArgumentException('Email cannot be empty'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (empty($firstName)) { |
79
|
|
|
throw new InvalidArgumentException('First name cannot be empty'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (empty($lastName)) { |
83
|
|
|
throw new InvalidArgumentException('Last name cannot be empty'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (empty($gender)) { |
87
|
|
|
throw new InvalidArgumentException('Gender cannot be empty'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$params = \array_merge([ |
91
|
|
|
'firstName' => $firstName, |
92
|
|
|
'lastName' => $lastName, |
93
|
|
|
'email' => $email, |
94
|
|
|
'gender' => $gender, |
95
|
|
|
], $optionalParams); |
96
|
|
|
|
97
|
|
|
return $params; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|