Completed
Pull Request — master (#14)
by Sergey
02:31
created

Endpoint::deleteResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\HeadHunterApi\EndPoints;
4
5
use seregazhuk\HeadHunterApi\Request;
6
7
abstract class Endpoint
8
{
9
    const RESOURCE = '/resource';
10
11
    /**
12
     * @var EndpointsContainer
13
     */
14
    protected $container;
15
16
    /**
17
     * @param EndpointsContainer $container
18
     */
19
    public function __construct(EndpointsContainer $container)
20
    {
21
        $this->container = $container;
22
    }
23
24
    /**
25
     * @param string $verb
26
     * @param array $params
27
     * @return array
28
     */
29
    protected function getResource($verb = '', array $params = [])
30
    {
31
        return $this->requestResource('get', $verb, $params);
32
    }
33
34
    /**
35
     * @param string $verb
36
     * @param array $params
37
     * @return mixed
38
     */
39
    protected function postResource($verb = '', array $params = [])
40
    {
41
        return $this->requestResource('post', $verb, $params);
42
    }
43
44
    /**
45
     * @param string $verb
46
     * @param array $params
47
     * @return mixed
48
     */
49
    protected function postResourceJson($verb = '', array $params = [])
50
    {
51
        return $this->requestResourceJson('post', $verb, $params);
52
    }
53
54
55
    /**
56
     * @param string $verb
57
     * @param array $params
58
     * @return mixed
59
     */
60
    protected function putResource($verb = '', array $params = [])
61
    {
62
        return $this->requestResource('put', $verb, $params);
63
    }
64
65
    /**
66
     * @param string $verb
67
     * @param array $params
68
     * @return mixed
69
     */
70
    protected function putResourceJson($verb = '', array $params = [])
71
    {
72
        return $this->requestResourceJson('put', $verb, $params);
73
    }
74
75
    /**
76
     * @param string $verb
77
     */
78
    protected function deleteResource($verb = '')
79
    {
80
        $this->requestResource('delete', $verb);
81
    }
82
83
    /**
84
     * @param string $method
85
     * @param string $verb
86
     * @param array $params
87
     * @return mixed
88
     */
89
    protected function requestResource($method = 'get', $verb = '', $params = [])
90
    {
91
        $method = strtolower($method);
92
93
        return $this->callRequestMethod($method, $verb, $params);
94
    }
95
96
    /**
97
     * @param string $method
98
     * @param string $verb
99
     * @param array $params
100
     * @return mixed
101
     */
102
    protected function requestResourceJson($method = 'get', $verb = '', $params = [])
103
    {
104
        return $this->requestResource($method. 'Json', $verb, $params);
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 Request
120
     */
121
    protected function getRequest()
122
    {
123
        return $this->request;
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
124
    }
125
126
    /**
127
     * @param $method
128
     * @param $verb
129
     * @param $params
130
     * @return mixed
131
     */
132
    protected function callRequestMethod($method, $verb, $params)
133
    {
134
        $request = $this->container->getRequest();
135
136
        return $request->$method(
137
            $this->getResourceUri($verb), $params
138
        );
139
    }
140
}