1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SafeCrow\Api; |
4
|
|
|
|
5
|
|
|
use SafeCrow\DataTransformer\CardDataTransformer; |
6
|
|
|
use SafeCrow\DataTransformer\OrderDataTransformer; |
7
|
|
|
use SafeCrow\DataTransformer\UserDataTransformer; |
8
|
|
|
use SafeCrow\Model\User; |
9
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class User |
13
|
|
|
* @package SafeCrow\Api |
14
|
|
|
*/ |
15
|
|
|
class UserApi extends AbstractApi |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param array $params |
19
|
|
|
* @return User |
20
|
|
|
*/ |
21
|
|
|
public function add(array $params): User |
22
|
|
|
{ |
23
|
|
|
$resolver = new OptionsResolver(); |
24
|
|
|
$resolver |
25
|
|
|
->setRequired([ |
26
|
|
|
'name', |
27
|
|
|
'phone', |
28
|
|
|
'email', |
29
|
|
|
]) |
30
|
|
|
->setDefault('accepts_conditions', true) |
31
|
|
|
->setAllowedTypes('name', 'string') |
32
|
|
|
->setAllowedTypes('phone', 'string') |
33
|
|
|
->setAllowedTypes('email', 'string') |
34
|
|
|
->setAllowedTypes('accepts_conditions', 'bool'); |
35
|
|
|
|
36
|
|
|
$params = $resolver->resolve($params); |
37
|
|
|
|
38
|
|
|
$response = $this->post('/users', $params); |
39
|
|
|
|
40
|
|
|
return $this->getResult($response, new UserDataTransformer()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param int $id |
45
|
|
|
* @param array $params |
46
|
|
|
* @return User |
47
|
|
|
*/ |
48
|
|
|
public function edit(int $id, array $params): User |
49
|
|
|
{ |
50
|
|
|
$resolver = new OptionsResolver(); |
51
|
|
|
$resolver |
52
|
|
|
->setDefaults([ |
53
|
|
|
'name' => null, |
54
|
|
|
'phone' => null, |
55
|
|
|
'email' => null, |
56
|
|
|
'accepts_conditions' => null, |
57
|
|
|
]) |
58
|
|
|
->setAllowedTypes('name', ['string', 'null']) |
59
|
|
|
->setAllowedTypes('phone', ['string', 'null']) |
60
|
|
|
->setAllowedTypes('email', ['string', 'null']) |
61
|
|
|
->setAllowedTypes('accepts_conditions', ['bool', 'null']); |
62
|
|
|
|
63
|
|
|
$params = array_filter($resolver->resolve($params)); |
64
|
|
|
|
65
|
|
|
$response = $this->post('/users/'.$id, $params); |
66
|
|
|
|
67
|
|
|
return $this->getResult($response, new UserDataTransformer()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $params |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function all(array $params = []): array |
75
|
|
|
{ |
76
|
|
|
$resolver = new OptionsResolver(); |
77
|
|
|
$resolver |
78
|
|
|
->setDefaults([ |
79
|
|
|
'email' => null, |
80
|
|
|
'phone' => null, |
81
|
|
|
]) |
82
|
|
|
->setAllowedTypes('email', ['string', 'null']) |
83
|
|
|
->setAllowedTypes('phone', ['string', 'null']); |
84
|
|
|
|
85
|
|
|
$params = array_filter($resolver->resolve($params)); |
86
|
|
|
|
87
|
|
|
$response = $this->get('/users', $params); |
88
|
|
|
|
89
|
|
|
return $this->getArrayResult($response, new UserDataTransformer()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param int $id |
94
|
|
|
* @return User |
95
|
|
|
*/ |
96
|
|
|
public function show(int $id): User |
97
|
|
|
{ |
98
|
|
|
$response = $this->get('/users/'.$id); |
99
|
|
|
|
100
|
|
|
return $this->getResult($response, new UserDataTransformer()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int $id |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function orders(int $id): array |
108
|
|
|
{ |
109
|
|
|
$response = $this->get('/users/'.$id.'/orders'); |
110
|
|
|
|
111
|
|
|
return $this->getArrayResult($response, new OrderDataTransformer()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $id |
116
|
|
|
* @param array $params |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function bind(int $id, array $params): string |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$resolver = new OptionsResolver(); |
122
|
|
|
$resolver |
123
|
|
|
->setRequired([ |
124
|
|
|
'redirect_url', |
125
|
|
|
]) |
126
|
|
|
->setAllowedTypes('redirect_url', 'string'); |
127
|
|
|
|
128
|
|
|
$params = $resolver->resolve($params); |
129
|
|
|
|
130
|
|
|
$response = $this->post('/users/'.$id.'/cards', $params); |
131
|
|
|
|
132
|
|
|
return $this->getSingleResult($response, 'redirect_url'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param int $id |
137
|
|
|
* @param array $params |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function cards(int $id, array $params = []): array |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$resolver = new OptionsResolver(); |
143
|
|
|
$resolver |
144
|
|
|
->setDefault('all', null) |
145
|
|
|
->setAllowedTypes('all', ['bool', 'null']); |
146
|
|
|
|
147
|
|
|
$params = array_filter($resolver->resolve($params)); |
148
|
|
|
|
149
|
|
|
$response = $this->get('/users/'.$id.'/cards', $params); |
150
|
|
|
|
151
|
|
|
return $this->getArrayResult($response, new CardDataTransformer()); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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.