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