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

Request::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
dl 0
loc 4
rs 10
c 3
b 1
f 1
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\HeadHunterApi;
4
5
6
use seregazhuk\HeadHunterApi\Contracts\HttpInterface;
7
use seregazhuk\HeadHunterApi\Contracts\RequestInterface;
8
use seregazhuk\HeadHunterApi\Exceptions\HeadHunterApiException;
9
10
class Request implements RequestInterface
11
{
12
    /**
13
     * @var HttpInterface
14
     */
15
    protected $client;
16
17
    public function __construct(HttpInterface $http, $token = null)
18
    {
19
        $this->client = $http;
20
21
        if($token) $this->client->setHeaders(['Authorization' => 'Bearer ' . $token]);
22
    }
23
24
    /**
25
     * @param string $uri
26
     * @param array $params
27
     * @return array|null
28
     */
29
    public function get($uri, $params = [])
30
    {
31
        return $this->client->get($uri, $params);
32
    }
33
34
    /**
35
     * @param string $uri
36
     * @param array $params
37
     * @return array
38
     */
39
    public function post($uri, $params = [])
40
    {
41
        return $this->client->post($uri, $params);
42
    }
43
44
    /**
45
     * @param string $uri
46
     * @param array $params
47
     * @return array
48
     */
49
    public function put($uri, $params = [])
50
    {
51
        return $this->client->put($uri, $params);
52
    }
53
54
    public function delete($uri)
55
    {
56
        return $this->client->delete($uri);
57
    }
58
59
    /**
60
     * @param string $requestMethod
61
     * @param string $uri
62
     * @param array $params
63
     * @param bool $useJson
64
     * @return mixed
65
     * @throws HeadHunterApiException
66
     */
67
    public function makeRequestCall($requestMethod, $uri, $params = [], $useJson = false)
68
    {
69
        $requestMethod = strtolower($requestMethod);
70
71
        if(!method_exists($this->client, $requestMethod)) {
72
            throw new HeadHunterApiException("Request method $requestMethod not found");
73
        }
74
75
        return $this->client->$requestMethod($uri, $params, $useJson);
76
    }
77
}