Passed
Pull Request — master (#3)
by Gombos
03:08 queued 42s
created

Api::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Glorand\Drip\Api;
4
5
use Glorand\Drip\Api\Response\ApiResponse;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\GuzzleException;
8
use GuzzleHttp\Psr7\Response;
9
10
abstract class Api
11
{
12
    const GET = 'get';
13
    const POST = 'post';
14
    const PUT = 'put';
15
    const DELETE = 'delete';
16
17
    /**
18
     * @var Client
19
     */
20
    protected $client;
21
22
    /**
23
     * Api constructor.
24
     *
25
     * @param Client $client
26
     */
27
    public function __construct(Client $client)
28
    {
29
        $this->client = $client;
30
    }
31
32
    protected function prepareUrl(string $url): string
33
    {
34
        return 'v2/'.$url;
35
    }
36
37
    /**
38
     * @param       $method
39
     * @param       $url
40
     * @param array $params
41
     *
42
     * @return ApiResponse
43
     */
44
    public function makeRequest($method, $url, $params = [])
45
    {
46
        $options = [];
47
        switch ($method) {
48
            case self::GET:
49
                $options['query'] = $params;
50
                break;
51
            case self::POST:
52
            case self::DELETE:
53
                // @codeCoverageIgnoreStart
54
            case self::PUT:
55
                // @codeCoverageIgnoreEnd
56
                $options['body'] = is_array($params) ? json_encode($params) : $params;
0 ignored issues
show
introduced by
The condition is_array($params) is always true.
Loading history...
57
                break;
58
            default:
59
                // @codeCoverageIgnoreStart
60
                $method = 'GET';
61
                break;
62
            // @codeCoverageIgnoreEnd
63
        }
64
65
        try {
66
            $response = $this->client->request($method, $url, $options);
67
        } catch (GuzzleException $e) {
68
            $response = new Response($e->getCode(), [], $e->getMessage());
69
        }
70
71
        return new ApiResponse($url, $options, $response);
72
    }
73
}
74