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
|
|
View Code Duplication |
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
|
|
|
private function validateAndGetParams(string $email, string $firstName, string $lastName, string $gender, array $optionalParams): array |
64
|
|
|
{ |
65
|
|
|
if (empty($email)) { |
66
|
|
|
throw new InvalidArgumentException('Email cannot be empty'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (empty($firstName)) { |
70
|
|
|
throw new InvalidArgumentException('First name cannot be empty'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (empty($lastName)) { |
74
|
|
|
throw new InvalidArgumentException('Last name cannot be empty'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (empty($gender)) { |
78
|
|
|
throw new InvalidArgumentException('Gender cannot be empty'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$params = \array_merge([ |
82
|
|
|
'firstName' => $firstName, |
83
|
|
|
'lastName' => $lastName, |
84
|
|
|
'email' => $email, |
85
|
|
|
'gender' => $gender, |
86
|
|
|
], $optionalParams); |
87
|
|
|
|
88
|
|
|
return $params; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.