1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\HeadHunterApi\EndPoints; |
4
|
|
|
|
5
|
|
|
use seregazhuk\HeadHunterApi\Contracts\RequestInterface; |
6
|
|
|
|
7
|
|
|
abstract class Endpoint |
8
|
|
|
{ |
9
|
|
|
const RESOURCE = '/resource'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var RequestInterface |
13
|
|
|
*/ |
14
|
|
|
protected $request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var EndpointsContainer |
18
|
|
|
*/ |
19
|
|
|
protected $container; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param EndpointsContainer $container |
23
|
|
|
*/ |
24
|
|
|
public function __construct(EndpointsContainer $container) |
25
|
|
|
{ |
26
|
|
|
$this->container = $container; |
27
|
|
|
$this->request = $container->getRequest(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $verb |
32
|
|
|
* @param array $params |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
protected function getResource($verb = '', array $params = []) |
36
|
|
|
{ |
37
|
|
|
return $this->requestResource('get', $verb, $params); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $verb |
42
|
|
|
* @param array $params |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
protected function postResource($verb = '', array $params = []) |
46
|
|
|
{ |
47
|
|
|
return $this->requestResource('post', $verb, $params); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $verb |
52
|
|
|
* @param array $params |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
protected function postResourceJson($verb = '', array $params = []) |
56
|
|
|
{ |
57
|
|
|
return $this->requestResourceJson('post', $verb, $params); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $verb |
63
|
|
|
* @param array $params |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
protected function putResource($verb = '', array $params = []) |
67
|
|
|
{ |
68
|
|
|
return $this->requestResource('put', $verb, $params); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $verb |
73
|
|
|
* @param array $params |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
protected function putResourceJson($verb = '', array $params = []) |
77
|
|
|
{ |
78
|
|
|
return $this->requestResourceJson('put', $verb, $params); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $verb |
83
|
|
|
*/ |
84
|
|
|
protected function deleteResource($verb = '') |
85
|
|
|
{ |
86
|
|
|
$this->requestResource('delete', $verb); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
protected function requestResource($method = 'get', $verb = '', $params = []) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$method = strtolower($method); |
92
|
|
|
|
93
|
|
|
return $this->request->makeRequest( |
94
|
|
|
$method, $this->getResourceUri($verb), $params |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
protected function requestResourceJson($method = 'get', $verb = '', $params = []) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$method = strtolower($method); |
101
|
|
|
|
102
|
|
|
return $this->request->makeRequest( |
103
|
|
|
$method, $this->getResourceUri($verb), $params |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $uri |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
protected function getResourceUri($uri = '') |
112
|
|
|
{ |
113
|
|
|
$resource = static::RESOURCE; |
114
|
|
|
|
115
|
|
|
return empty($uri) ? $resource : $resource . sprintf('/%s', $uri); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return RequestInterface |
120
|
|
|
*/ |
121
|
|
|
protected function getRequest() |
122
|
|
|
{ |
123
|
|
|
return $this->request; |
124
|
|
|
} |
125
|
|
|
} |
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.