Passed
Push — master ( d90ced...ae50da )
by Robin
09:47
created

GuzzleClientFactory::buildAuthenticationValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Klever\JustGivingApiSdk\Support;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\HandlerStack;
7
use GuzzleHttp\Middleware;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Class GuzzleClientFactory
12
 *
13
 * Helper class to instantiate a Guzzle HTTP client with the correct configuration for passing into the JustGiving
14
 * client.
15
 */
16
class GuzzleClientFactory
17
{
18
    /** @var string */
19
    protected $apiKey;
20
21
    /** @var string */
22
    protected $apiVersion;
23
24
    /** @var string */
25
    protected $username;
26
27
    /** @var string */
28
    protected $password;
29
30
    /** @var string */
31
    protected $rootDomain;
32
33
    /** @var array */
34
    protected $userOptions;
35
36
    /**
37
     * GuzzleClientFactory constructor.
38
     *
39
     * @param string $rootDomain
40
     * @param string $apiKey
41
     * @param string $apiVersion
42
     * @param string $username
43
     * @param string $password
44
     * @param array  $options
45
     */
46 89
    public function __construct($rootDomain, $apiKey, $apiVersion, $username = '', $password = '', $options = [])
47
    {
48 89
        $this->rootDomain = $rootDomain;
49 89
        $this->apiKey = $apiKey;
50 89
        $this->apiVersion = $apiVersion;
51 89
        $this->username = $username;
52 89
        $this->password = $password;
53 89
        $this->userOptions = $options;
54 89
    }
55
56
    /**
57
     * Static method for easily creating a client. Requires the same parameters as the class constructor.
58
     *
59
     * @param array ...$args
60
     * @return Client
61
     */
62 88
    public static function build(...$args)
63
    {
64 88
        return (new static(...$args))->createClient();
0 ignored issues
show
Bug introduced by
The call to GuzzleClientFactory::__construct() misses some required arguments starting with $apiKey.
Loading history...
Documentation introduced by
$args is of type array<integer,array>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
    }
66
67
    /**
68
     * Set up the handler stack with middleware, configure options and instantiate the Guzzle client.
69
     *
70
     * @return Client
71
     */
72 88
    public function createClient()
73
    {
74 88
        $stack = HandlerStack::create();
75 88
        $stack->push(Middleware::mapResponse(function(ResponseInterface $response) {
76 74
            return new Response($response);
77 88
        }));
78
79 88
        return new Client(array_merge([
80 88
            'http_errors' => false,
81 88
            'handler'     => $stack,
82 88
            'base_uri'    => $this->baseUrl(),
83
            'headers'     => [
84 88
                'Accept'        => 'application/json',
85 88
                'Authorize'     => 'Basic ' . $this->buildAuthenticationValue(),
86 88
                'Authorization' => 'Basic ' . $this->buildAuthenticationValue(),
87
            ]
88 88
        ], $this->userOptions));
89
    }
90
91
    /**
92
     * Return the base URL string for the API call.
93
     *
94
     * @return string
95
     */
96 88
    public function baseUrl()
97
    {
98 88
        return $this->rootDomain . $this->apiKey . '/v' . $this->apiVersion . '/';
99
    }
100
101
    /**
102
     * Build the base 64 encoded string that contains authentication credentials.
103
     *
104
     * @return string
105
     */
106 89
    protected function buildAuthenticationValue()
107
    {
108 89
        return empty($this->username)
109 1
            ? ''
110 89
            : base64_encode($this->username . ":" . $this->password);
111
    }
112
}
113