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

Request::executeRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
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 $requestMethod
26
     * @param string $uri
27
     * @param array $params
28
     * @param bool $useJson
29
     * @return mixed
30
     * @throws HeadHunterApiException
31
     */
32
    public function makeRequest($requestMethod, $uri, $params = [], $useJson = false)
33
    {
34
        $requestMethod = strtolower($requestMethod);
35
36
        return $this->executeRequest($requestMethod, $uri, $params);
37
    }
38
39
    /**
40
     * @param string $requestMethod
41
     * @param string $uri
42
     * @param array $params
43
     * @return mixed
44
     * @throws HeadHunterApiException
45
     */
46
    public function makeJsonRequest($requestMethod, $uri, $params = [])
47
    {
48
        $requestMethod = strtolower($requestMethod) . 'json';
49
50
        return $this->executeRequest($requestMethod, $uri, $params);
51
    }
52
53
    /**
54
     * @param $requestMethod
55
     * @param $uri
56
     * @param array $params
57
     * @return mixed
58
     * @throws HeadHunterApiException
59
     */
60
    protected function executeRequest($requestMethod, $uri, array $params = [])
61
    {
62
        if(!method_exists($this->client, $requestMethod)) {
63
            throw new HeadHunterApiException("Request method $requestMethod not found");
64
        }
65
66
        return $this->client->$requestMethod($uri, $params);
67
    }
68
}