Completed
Push — master ( 7e4f78...5ca817 )
by Filippo
02:47
created

GuzzleHttpAdapter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 122
Duplicated Lines 50.82 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 62
loc 122
c 0
b 0
f 0
wmc 18
lcom 1
cbo 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 4
A get() 10 10 2
A delete() 10 10 2
A put() 12 12 3
A post() 12 12 3
A getLatestResponseHeaders() 11 11 2
A handleError() 7 7 2

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
/*
4
 * This file is part of Laravel SMSFactor.
5
 *
6
 * (c) Filippo Galante <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace IlGala\SMSFactor\Adapters;
13
14
use GuzzleHttp\Client;
15
use GuzzleHttp\ClientInterface;
16
use GuzzleHttp\Exception\RequestException;
17
use GuzzleHttp\Message\ResponseInterface;
18
use GuzzleHttp\Psr7\Response;
19
use IlGala\SMSFactor\Exceptions\HttpException;
20
21
/**
22
 * @author Filippo Galante <[email protected]>
23
 */
24
class GuzzleHttpAdapter implements AdapterInterface
25
{
26
27
    /**
28
     * @var ClientInterface
29
     */
30
    protected $client;
31
32
    /**
33
     * @var Response|ResponseInterface
34
     */
35
    protected $response;
36
37
    /**
38
     * @param string               $username
39
     * @param string               $password
40
     * @param string               $accept
41
     * @param ClientInterface|null $client
42
     */
43
    public function __construct($username, $password, $accept, ClientInterface $client = null)
44
    {
45
        if (version_compare(ClientInterface::VERSION, '6') === 1) {
46
            $this->client = $client ?: new Client(['headers' => [
0 ignored issues
show
Documentation Bug introduced by
It seems like $client ?: new \GuzzleHt... 'Accept' => $accept))) can also be of type object<GuzzleHttp\Client>. However, the property $client is declared as type object<GuzzleHttp\ClientInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
                    'sfusername' => $username,
48
                    'sfpassword' => $password,
49
                    'Accept' => $accept
50
            ]]);
51
        } else {
52
            $this->client = $client ?: new Client();
0 ignored issues
show
Documentation Bug introduced by
It seems like $client ?: new \GuzzleHttp\Client() can also be of type object<GuzzleHttp\Client>. However, the property $client is declared as type object<GuzzleHttp\ClientInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
53
            $this->client->setDefaultOption('headers/sfusername', $username);
54
            $this->client->setDefaultOption('headers/sfpassword', $password);
55
            $this->client->setDefaultOption('headers/Accept', $accept);
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 View Code Duplication
    public function get($url)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
63
    {
64
        try {
65
            $this->response = $this->client->get($url);
66
        } catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
67
            $this->response = $e->getResponse();
68
            $this->handleError();
69
        }
70
        return $this->response->getBody();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 View Code Duplication
    public function delete($url)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
77
    {
78
        try {
79
            $this->response = $this->client->delete($url);
80
        } catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
81
            $this->response = $e->getResponse();
82
            $this->handleError();
83
        }
84
        return $this->response->getBody();
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 View Code Duplication
    public function put($url, $content = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
91
    {
92
        $options = [];
93
        $options[is_array($content) ? 'json' : 'body'] = $content;
94
        try {
95
            $this->response = $this->client->put($url, $options);
96
        } catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
97
            $this->response = $e->getResponse();
98
            $this->handleError();
99
        }
100
        return $this->response->getBody();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 View Code Duplication
    public function post($url, $content = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
107
    {
108
        $options = [];
109
        $options[is_array($content) ? 'json' : 'body'] = $content;
110
        try {
111
            $this->response = $this->client->post($url, $options);
112
        } catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
113
            $this->response = $e->getResponse();
114
            $this->handleError();
115
        }
116
        return $this->response->getBody();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 View Code Duplication
    public function getLatestResponseHeaders()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
123
    {
124
        if (null === $this->response) {
125
            return;
126
        }
127
        return [
128
            'reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'),
129
            'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'),
130
            'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit'),
131
        ];
132
    }
133
134
    /**
135
     * @throws HttpException
136
     */
137 View Code Duplication
    protected function handleError()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
138
    {
139
        $body = (string) $this->response->getBody();
140
        $code = (int) $this->response->getStatusCode();
141
        $content = json_decode($body);
142
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
143
    }
144
145
}
146