Completed
Pull Request — master (#12)
by Sergey
02:44
created

Endpoint::postResource()   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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
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 $uri
32
     * @return string
33
     */
34
    protected function getResourceUri($uri = '')
35
    {
36
        $resource = static::RESOURCE;
37
38
        return empty($uri) ? $resource : $resource . sprintf('/%s', $uri);
39
    }
40
41
    protected function requestResource($method = 'get', $verb = '', $params = [])
42
    {
43
        $method = strtolower($method);
44
45
        return $this->request->makeRequestCall(
46
            $method, $this->getResourceUri($verb), $params
47
        );
48
    }
49
50
    /**
51
     * @param string $verb
52
     * @param array $params
53
     * @return array
54
     */
55
    protected function getResource($verb = '', array $params = [])
56
    {
57
        return $this->requestResource('get', $verb, $params);
58
    }
59
60
    /**
61
     * @param string $verb
62
     * @param array $params
63
     * @return mixed
64
     */
65
    protected function postResource($verb = '', array $params = [])
66
    {
67
        return $this->requestResource('post', $verb, $params);
68
    }
69
70
    /**
71
     * @param string $verb
72
     */
73
    protected function deleteResource($verb = '')
74
    {
75
        $this->requestResource('delete', $verb);
76
    }
77
78
    /**
79
     * @return RequestInterface
80
     */
81
    protected function getRequest()
82
    {
83
        return $this->request;
84
    }
85
}