Passed
Pull Request — master (#3)
by Gombos
02:04
created

ClientManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Glorand\Drip;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\HandlerStack;
7
8
abstract class ClientManager
9
{
10
    /** @var string */
11
    private $apiToken;
12
    /** @var string */
13
    private $userAgent;
14
    /** @var string */
15
    private $apiEndPoint = 'https://api.getdrip.com/v2/';
16
    /** @var Client */
17
    private $client = null;
18
    /** @var HandlerStack */
19
    private $handler;
20
21
    public function __construct(string $apiToken, string $userAgent)
22
    {
23
        $this->apiToken = $apiToken;
24
        $this->userAgent = $userAgent;
25
    }
26
27
    /**
28
     * @param array $options
29
     *
30
     * @return Client
31
     */
32
    public function getClient(array $options = [])
33
    {
34
        if (!is_null($this->client)) {
35
            return $this->client;
36
        }
37
38
        $options = array_merge(
39
            ['handler' => $this->createClientHandler()],
40
            $options,
41
            [
42
                'base_uri' => $this->apiEndPoint,
43
                'auth'     => [
44
                    $this->apiToken,
45
                    '',
46
                ],
47
                'headers'  => [
48
                    'Accept'       => 'application/vnd.api+json',
49
                    'Content-Type' => 'application/vnd.api+json',
50
                    'User-Agent'   => $this->userAgent,
51
                ],
52
            ]
53
        );
54
        $this->client = new Client($options);
55
56
        return $this->client;
57
    }
58
59
    public function setClient(Client $client): self
60
    {
61
        $this->client = $client;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param callable|null $handler
68
     */
69
    public function setHandler(callable $handler = null)
70
    {
71
        $this->handler = $handler;
0 ignored issues
show
Documentation Bug introduced by
It seems like $handler can also be of type callable. However, the property $handler is declared as type GuzzleHttp\HandlerStack. 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...
72
    }
73
74
    /**
75
     * @return HandlerStack
76
     */
77
    private function createClientHandler(): HandlerStack
78
    {
79
        $stack = HandlerStack::create();
80
        if ($this->handler) {
81
            $stack->setHandler($this->handler);
82
        }
83
84
        return $stack;
85
    }
86
}
87