BuzzAdapter::post()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
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 Buzz\Browser;
15
use Buzz\Client\Curl;
16
use Buzz\Client\FileGetContents;
17
use Buzz\Message\Response;
18
use IlGala\SMSFactor\Exceptions\HttpException;
19
20
/**
21
 * @author Filippo Galante <[email protected]>
22
 */
23
class BuzzAdapter implements AdapterInterface
24
{
25
26
    /**
27
     * @var Browser
28
     */
29
    protected $browser;
30
31
    /**
32
     * @var string
33
     */
34
    protected $username;
35
36
    /**
37
     * @var string
38
     */
39
    protected $password;
40
41
    /**
42
     * @var string
43
     */
44
    protected $accept;
45
46
    /**
47
     * @param string                 $username
48
     * @param string                 $password
49
     * @param string                 $accept
50
     * @param Browser|null           $browser
51
     */
52
    public function __construct($username, $password, $accept, Browser $browser = null)
53
    {
54
        $this->browser = $browser ?: new Browser(function_exists('curl_exec') ? new Curl() : new FileGetContents());
55
        $this->username = $username;
56
        $this->password = $password;
57
        $this->accept = $accept;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function get($url)
64
    {
65
        $headers = [
66
            'sfusername: ' . $this->username,
67
            'sfpassword: ' . $this->password,
68
            'Accept: ' . $this->accept,
69
        ];
70
        $response = $this->browser->get($url, $headers);
71
        $this->handleResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Buzz\Message\MessageInterface> is not a sub-type of object<Buzz\Message\Response>. It seems like you assume a concrete implementation of the interface Buzz\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
72
        return $response->getContent();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function delete($url)
79
    {
80
        $response = $this->browser->delete($url);
81
        $this->handleResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Buzz\Message\MessageInterface> is not a sub-type of object<Buzz\Message\Response>. It seems like you assume a concrete implementation of the interface Buzz\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 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...
88
    {
89
        $headers = [
90
            'sfusername: ' . $this->username,
91
            'sfpassword: ' . $this->password,
92
            'Accept: ' . $this->accept,
93
        ];
94
        if (is_array($content)) {
95
            $content = json_encode($content);
96
            $headers[] = 'Content-Type: application/json';
97
        }
98
        $response = $this->browser->put($url, $headers, $content);
99
        $this->handleResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Buzz\Message\MessageInterface> is not a sub-type of object<Buzz\Message\Response>. It seems like you assume a concrete implementation of the interface Buzz\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
100
        return $response->getContent();
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
        $headers = [
109
            'sfusername: ' . $this->username,
110
            'sfpassword: ' . $this->password,
111
            'Accept: ' . $this->accept,
112
        ];
113
        if (is_array($content)) {
114
            $content = json_encode($content);
115
            $headers[] = 'Content-Type: application/json';
116
        }
117
        $response = $this->browser->post($url, $headers, $content);
118
        $this->handleResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Buzz\Message\MessageInterface> is not a sub-type of object<Buzz\Message\Response>. It seems like you assume a concrete implementation of the interface Buzz\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
119
        return $response->getContent();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getLatestResponseHeaders()
126
    {
127
        if (null === $response = $this->browser->getLastResponse()) {
128
            return;
129
        }
130
        return [
131
            'reset' => (int) $response->getHeader('RateLimit-Reset'),
132
            'remaining' => (int) $response->getHeader('RateLimit-Remaining'),
133
            'limit' => (int) $response->getHeader('RateLimit-Limit'),
134
        ];
135
    }
136
137
    /**
138
     * @param Response $response
139
     *
140
     * @throws HttpException
141
     */
142
    protected function handleResponse(Response $response)
143
    {
144
        if ($response->isSuccessful()) {
145
            return;
146
        }
147
        $this->handleError($response);
148
    }
149
150
    /**
151
     * @param Response $response
152
     *
153
     * @throws HttpException
154
     */
155
    protected function handleError(Response $response)
156
    {
157
        $body = (string) $response->getContent();
158
        $code = (int) $response->getStatusCode();
159
        $content = json_decode($body);
160
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
161
    }
162
}
163