Completed
Push — master ( fb0370...84a128 )
by Ronaldo
03:03
created

Client::post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 6
1
<?php
2
3
namespace WSW\SiftScience\Client;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Client as HttpClient;
7
use GuzzleHttp\Exception\RequestException;
8
use Psr\Http\Message\ResponseInterface;
9
use WSW\SiftScience\Collections\Headers;
10
use WSW\SiftScience\Exceptions\SiftScienceRequestException;
11
12
/**
13
 * Class Client
14
 *
15
 * @package WSW\SiftScience\Client
16
 * @author Ronaldo Matos Rodrigues <[email protected]>
17
 */
18
class Client
19
{
20
    /**
21
     * @var \GuzzleHttp\Client
22
     */
23
    private $client;
24
25
    /**
26
     * @var Headers
27
     */
28
    private $headers;
29
30
    /**
31
     * @param ClientInterface $client
32
     */
33
    public function __construct(ClientInterface $client = null)
34
    {
35
        $this->client = $client ?: new HttpClient();
0 ignored issues
show
Documentation Bug introduced by
$client ?: new \GuzzleHttp\Client() is of type object<GuzzleHttp\ClientInterface>, but the property $client was declared to be of type object<GuzzleHttp\Client>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
36
        $this->headers = new Headers();
37
        $this->addHeader('Content-Type', 'application/json');
38
        $this->addHeader('Accept', 'application/json');
39
    }
40
41
    /**
42
     * @param string $key
43
     * @param string $value
44
     *
45
     * @return $this
46
     */
47
    public function addHeader($key, $value)
48
    {
49
        $this->headers->add([$key => $value]);
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param string $url
56
     * @param string $body
57
     * @throws SiftScienceRequestException
58
     *
59
     * @return ResponseInterface
60
     */
61 View Code Duplication
    public function post($url, $body)
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...
62
    {
63
        try {
64
            $response = $this->client->request('POST', $url, [
65
                'headers' => $this->headers->getValues(),
66
                'body'    => $body,
67
                'verify'  => false
68
            ]);
69
70
            return $response;
71
        } catch (RequestException $e) {
72
            throw new SiftScienceRequestException($e);
73
        }
74
    }
75
76
    /**
77
     * @param string $url
78
     * @throws SiftScienceRequestException
79
     *
80
     * @return ResponseInterface
81
     */
82 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...
83
    {
84
        try {
85
            $response = $this->client->request('GET', $url, [
86
                'headers' => $this->headers->getValues(),
87
                'verify' => false
88
            ]);
89
90
            return $response;
91
        } catch (RequestException $e) {
92
            throw new SiftScienceRequestException($e);
93
        }
94
    }
95
}
96