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

Endpoint   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 91
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getResource() 0 4 1
A postResource() 0 4 1
A putResource() 0 4 1
A deleteResource() 0 4 1
A requestResource() 0 8 1
A getResourceUri() 0 6 2
A getRequest() 0 4 1
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
     * @param $useJson
44
     * @return mixed
45
     */
46
    protected function postResource($verb = '', array $params = [], $useJson = false)
47
    {
48
        return $this->requestResource('post', $verb, $params, $useJson);
49
    }
50
51
    /**
52
     * @param string $verb
53
     * @param array $params
54
     * @param bool $useJson
55
     * @return mixed
56
     */
57
    protected function putResource($verb = '', array $params = [], $useJson = false)
58
    {
59
        return $this->requestResource('put', $verb, $params, $useJson);
60
    }
61
62
    /**
63
     * @param string $verb
64
     */
65
    protected function deleteResource($verb = '')
66
    {
67
        $this->requestResource('delete', $verb);
68
    }
69
70
    protected function requestResource($method = 'get', $verb = '', $params = [], $useJson = false)
71
    {
72
        $method = strtolower($method);
73
74
        return $this->request->makeRequestCall(
75
            $method, $this->getResourceUri($verb), $params, $useJson
76
        );
77
    }
78
79
    /**
80
     * @param string $uri
81
     * @return string
82
     */
83
    protected function getResourceUri($uri = '')
84
    {
85
        $resource = static::RESOURCE;
86
87
        return empty($uri) ? $resource : $resource . sprintf('/%s', $uri);
88
    }
89
90
    /**
91
     * @return RequestInterface
92
     */
93
    protected function getRequest()
94
    {
95
        return $this->request;
96
    }
97
}