1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TestMonitor\Custify\Actions; |
4
|
|
|
|
5
|
|
|
use TestMonitor\Custify\Validator; |
6
|
|
|
use TestMonitor\Custify\Resources\Person; |
7
|
|
|
use TestMonitor\Custify\Transforms\TransformsPeople; |
8
|
|
|
use TestMonitor\Custify\Exceptions\NotFoundException; |
9
|
|
|
|
10
|
|
|
trait ManagesPeople |
11
|
|
|
{ |
12
|
|
|
use TransformsPeople; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Get a list of of people. |
16
|
|
|
* |
17
|
|
|
* @param int $page |
18
|
|
|
* @param int $limit |
19
|
|
|
* |
20
|
|
|
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException |
21
|
|
|
* @return \TestMonitor\Custify\Resources\Person[] |
22
|
|
|
*/ |
23
|
7 |
View Code Duplication |
public function people($page = 1, $limit = 10): array |
|
|
|
|
24
|
|
|
{ |
25
|
7 |
|
$response = $this->get('people/all', [ |
|
|
|
|
26
|
7 |
|
'query' => ['itemsPerPage' => $limit, 'page' => $page], |
27
|
|
|
]); |
28
|
|
|
|
29
|
1 |
|
return $this->fromCustifyPeople($response[0]['people']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get a single person. |
34
|
|
|
* |
35
|
|
|
* @param string $id |
36
|
|
|
* |
37
|
|
|
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException |
38
|
|
|
* @return \TestMonitor\Custify\Resources\Person |
39
|
|
|
*/ |
40
|
2 |
|
public function person(string $id): Person |
41
|
|
|
{ |
42
|
2 |
|
$response = $this->get("people/{$id}"); |
|
|
|
|
43
|
|
|
|
44
|
1 |
|
return $this->fromCustifyPerson($response); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get a single person by user id. |
49
|
|
|
* |
50
|
|
|
* @param string $userId |
51
|
|
|
* |
52
|
|
|
* @throws \TestMonitor\Custify\Exceptions\NotFoundException |
53
|
|
|
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException |
54
|
|
|
* @return \TestMonitor\Custify\Resources\Person |
55
|
|
|
*/ |
56
|
2 |
View Code Duplication |
public function personByUserId(string $userId): Person |
|
|
|
|
57
|
|
|
{ |
58
|
2 |
|
$response = $this->get("people?user_id={$userId}"); |
|
|
|
|
59
|
|
|
|
60
|
2 |
|
Validator::keysExists($response[0], ['people']); |
61
|
|
|
|
62
|
|
|
// Simulate a not found response when the user_id does not exists. |
63
|
2 |
|
if (empty($response[0]['people'])) { |
64
|
1 |
|
throw new NotFoundException(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return $this->fromCustifyPerson($response[0]['people'][0]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get a single person by email. |
72
|
|
|
* |
73
|
|
|
* @param string $email |
74
|
|
|
* |
75
|
|
|
* @throws \TestMonitor\Custify\Exceptions\NotFoundException |
76
|
|
|
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException |
77
|
|
|
* @return \TestMonitor\Custify\Resources\Person |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function personByEmail(string $email): Person |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$response = $this->get("people?email={$email}"); |
|
|
|
|
82
|
|
|
|
83
|
|
|
Validator::keysExists($response[0], ['people']); |
84
|
|
|
|
85
|
|
|
// Simulate a not found response when the user_id does not exists. |
86
|
|
|
if (empty($response[0]['people'])) { |
87
|
|
|
throw new NotFoundException(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->fromCustifyPerson($response[0]['people'][0]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create or update a person. |
95
|
|
|
* |
96
|
|
|
* @param \TestMonitor\Custify\Resources\Person $person |
97
|
|
|
* |
98
|
|
|
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException |
99
|
|
|
* @return \TestMonitor\Custify\Resources\Person |
100
|
|
|
*/ |
101
|
1 |
|
public function createOrUpdatePerson(Person $person): Person |
102
|
|
|
{ |
103
|
1 |
|
$response = $this->post('people', ['json' => $this->toCustifyPerson($person)]); |
|
|
|
|
104
|
|
|
|
105
|
1 |
|
return $this->fromCustifyPerson($response); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Delete a person. |
110
|
|
|
* |
111
|
|
|
* @param \TestMonitor\Custify\Resources\Person $person |
112
|
|
|
* |
113
|
|
|
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException |
114
|
|
|
* @return \TestMonitor\Custify\Resources\Person |
115
|
|
|
*/ |
116
|
|
|
public function deletePerson(Person $person): Person |
117
|
|
|
{ |
118
|
|
|
return $this->delete("people/{$person->id}"); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.