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

AbstractApi   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 8.65 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 9
loc 104
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstapushClient() 0 4 1
A setInstapushClient() 0 4 1
A transformResponse() 0 15 4
A doGet() 0 13 2
B handleException() 9 26 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Exception\ApiException;
14
use Fnayou\InstapushPHP\InstapushClient;
15
use Fnayou\InstapushPHP\Model\Error;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * Class AbstractApi.
20
 */
21
abstract class AbstractApi
22
{
23
    /** @var \Fnayou\InstapushPHP\InstapushClient */
24
    protected $instapushClient;
25
26
    /**
27
     * @param \Fnayou\InstapushPHP\InstapushClient $instapushClient
28
     */
29
    public function __construct(InstapushClient $instapushClient)
30
    {
31
        $this->instapushClient = $instapushClient;
32
    }
33
34
    /**
35
     * @return \Fnayou\InstapushPHP\InstapushClient
36
     */
37
    protected function getInstapushClient()
38
    {
39
        return $this->instapushClient;
40
    }
41
42
    /**
43
     * @param \Fnayou\InstapushPHP\InstapushClient $instapushClient
44
     */
45
    protected function setInstapushClient(InstapushClient $instapushClient)
46
    {
47
        $this->instapushClient = $instapushClient;
48
    }
49
50
    /**
51
     * @param \Psr\Http\Message\ResponseInterface $response
52
     * @param string                              $class
53
     *
54
     * @return mixed|\Psr\Http\Message\ResponseInterface
55
     */
56
    protected function transformResponse(ResponseInterface $response, $class)
57
    {
58
        // return \GuzzleHttp\Psr7\Response
59
        if (!$this->getInstapushClient()->getTransformer()) {
60
            return $response;
61
        }
62
63
        // handle exception
64
        if (200 !== $response->getStatusCode() || 201 !== $response->getStatusCode()) {
65
            $this->handleException($response);
66
        }
67
68
        // transform response according to class
69
        return $this->getInstapushClient()->getTransformer()->transform($response, $class);
70
    }
71
72
    /**
73
     * @param string $path
74
     * @param array  $parameters
75
     * @param array  $headers
76
     *
77
     * @return \Psr\Http\Message\ResponseInterface
78
     */
79
    protected function doGet(string $path, array $parameters = [], array $headers = [])
80
    {
81
        if (0 < \count($parameters)) {
82
            $path .= '?'.\http_build_query($parameters);
83
        }
84
85
        $request = $this
86
            ->getInstapushClient()
87
            ->getRequestFactory()
88
            ->createRequest('GET', $path, $headers);
89
90
        return $this->instapushClient->getHttpClient()->sendRequest($request);
91
    }
92
93
    /**
94
     * @param \Psr\Http\Message\ResponseInterface $response
95
     *
96
     * @return mixed
97
     */
98
    protected function handleException(ResponseInterface $response)
99
    {
100
        if (false === $this->getInstapushClient()->isException()) {
101
            return $this->getInstapushClient()->getTransformer()->transform($response, Error::class);
102
        }
103
104 View Code Duplication
        if (0 !== \strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
            throw new ApiException(
106
                \sprintf(
107
                    'Waiting for json response but %s given',
108
                    $response->getHeaderLine('Content-Type')
109
                ),
110
                500
111
            );
112
        }
113
114
        $content = \json_decode($response->getBody()->getContents(), true);
115
116
        if (true === isset($content['message'])) {
117
            $message = $content['message'];
118
        } else {
119
            $message = 'An unexpected/unknown error occurred.';
120
        }
121
122
        throw new ApiException($message, $response->getStatusCode(), $response);
123
    }
124
}
125