Completed
Pull Request — master (#3)
by Aymen
02:08
created

AbstractApi   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 54
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstapushClient() 0 4 1
A setInstapushClient() 0 4 1
A transformResponse() 0 10 2
A doGet() 0 13 2
1
<?php
2
/**
3
 * This file is part of the fnayou/instapush-php project.
4
 *
5
 * Copyright (c) 2017. Aymen FNAYOU <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Fnayou\InstapushPHP\Api;
12
13
use Fnayou\InstapushPHP\InstapushClient;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * Class AbstractApi.
18
 */
19
abstract class AbstractApi
20
{
21
    /** @var \Fnayou\InstapushPHP\InstapushClient */
22
    protected $instapushClient;
23
24
    /**
25
     * @param \Fnayou\InstapushPHP\InstapushClient $instapushClient
26
     */
27
    public function __construct(InstapushClient $instapushClient)
28
    {
29
        $this->instapushClient = $instapushClient;
30
    }
31
32
    /**
33
     * @return \Fnayou\InstapushPHP\InstapushClient
34
     */
35
    protected function getInstapushClient()
36
    {
37
        return $this->instapushClient;
38
    }
39
40
    /**
41
     * @param \Fnayou\InstapushPHP\InstapushClient $instapushClient
42
     */
43
    protected function setInstapushClient(InstapushClient $instapushClient)
44
    {
45
        $this->instapushClient = $instapushClient;
46
    }
47
48
    protected function transformResponse(ResponseInterface $response, $class)
49
    {
50
        if (!$this->getInstapushClient()->getTransformer()) {
51
            return $response;
52
        }
53
54
        //TODO: handle errors
55
56
        return $this->getInstapushClient()->getTransformer()->transform($response, $class);
57
    }
58
59
    protected function doGet(string $path, array $parameters = [], array $headers = [])
60
    {
61
        if (count($parameters) > 0) {
62
            $path .= '?'.\http_build_query($parameters);
63
        }
64
65
        $request = $this
66
            ->getInstapushClient()
67
            ->getRequestFactory()
68
            ->createRequest('GET', $path, $headers);
69
70
        return $this->instapushClient->getHttpClient()->sendRequest($request);
71
    }
72
}
73