Client::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
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 HttpClient
22
     */
23
    private $client;
24
25
    /**
26
     * @var Headers
27
     */
28
    private $headers;
29
30
    /**
31
     * @param ClientInterface $client
32
     */
33 15
    public function __construct(ClientInterface $client = null)
34
    {
35 15
        $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 15
        $this->headers = new Headers();
37 15
    }
38
39
    /**
40
     * @param string $key
41
     * @param string $value
42
     *
43
     * @return $this
44
     */
45 1
    public function addHeader($key, $value)
46
    {
47 1
        $this->headers->add([$key => $value]);
48
49 1
        return $this;
50
    }
51
52
    /**
53
     * @param string $url
54
     * @param string $body
55
     * @throws SiftScienceRequestException
56
     *
57
     * @return ResponseInterface
58
     */
59 8
    public function post($url, $body)
60
    {
61
        try {
62 8
            $response = $this->client->request('POST', $url, [
63 8
                'headers' => $this->headers->getValues(),
64 8
                'body'    => $body,
65
                'verify'  => false
66
            ]);
67
68 1
            return $response;
69
70 7
        } catch (RequestException $e) {
71 7
            throw new SiftScienceRequestException($e);
72
        }
73
    }
74
}
75