Completed
Push — master ( f99eff...e99cc7 )
by Zbigniew
13:22
created

GuzzleClient::setBearerToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the WrikePhpGuzzle package.
4
 *
5
 * (c) Zbigniew Ślązak
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zibios\WrikePhpGuzzle\Client;
12
13
use GuzzleHttp\Client as BaseClient;
14
use Psr\Http\Message\ResponseInterface;
15
use Zibios\WrikePhpLibrary\Client\ClientInterface;
16
use Zibios\WrikePhpLibrary\Enum\Api\RequestMethodEnum;
17
use Zibios\WrikePhpLibrary\Exception\Api\ApiException;
18
use Zibios\WrikePhpLibrary\Transformer\ApiExceptionTransformerInterface;
19
20
/**
21
 * Guzzle Client
22
 */
23
class GuzzleClient extends BaseClient implements ClientInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $bearerToken = '';
29
30
    /**
31
     * @var ApiExceptionTransformerInterface
32
     */
33
    protected $apiExceptionTransformer;
34
35
    /**
36
     * Client constructor.
37
     *
38
     * @param ApiExceptionTransformerInterface $apiExceptionTransformer
39
     * @param array $config
40
     */
41 4
    public function __construct(ApiExceptionTransformerInterface $apiExceptionTransformer, array $config = [])
42
    {
43 4
        $this->apiExceptionTransformer = $apiExceptionTransformer;
44 4
        parent::__construct($config);
45 4
    }
46
47
    /**
48
     * @return string
49
     */
50 2
    public function getBearerToken()
51
    {
52 2
        return $this->bearerToken;
53
    }
54
55
    /**
56
     * @param string $bearerToken
57
     *
58
     * @return $this
59
     * @throws \InvalidArgumentException
60
     */
61 2
    public function setBearerToken($bearerToken)
62
    {
63 2
        if (is_string($bearerToken) === false) {
64 1
            throw new \InvalidArgumentException('Bearer Token should be a string!');
65 1
        }
66 1
        $this->bearerToken = $bearerToken;
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * @param string $requestMethod
73
     * @param string $path
74
     * @param array $params
75
     *
76
     * @return ResponseInterface
77
     * @throws \InvalidArgumentException
78
     * @throws \Exception|ApiException
79
     */
80
    public function executeRequestForParams($requestMethod, $path, array $params)
81
    {
82
        $options = $this->calculateOptionsForParams($requestMethod, $params);
83
84
        return $this->request($requestMethod, $path, $options);
85
    }
86
87
    /**
88
     * @param string $requestMethod
89
     * @param array $params
90
     *
91
     * @return array
1 ignored issue
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,string>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
92
     * @throws \InvalidArgumentException
93
     */
94
    protected function calculateOptionsForParams($requestMethod, array $params)
95
    {
96
        $requestMethod = strtoupper($requestMethod);
97
        $options = [];
98
99
        switch ($requestMethod) {
100
            case RequestMethodEnum::GET:
101
                if (count($params) > 0) {
102
                    $options['query'] = $params;
103
                }
104
                break;
105
            case RequestMethodEnum::PUT:
106
            case RequestMethodEnum::POST:
107
                if (count($params) > 0) {
108
                    $options['json'] = $params;
109
                }
110
                break;
111
            case RequestMethodEnum::DELETE:
112
                break;
113
            default:
114
                throw new \InvalidArgumentException();
115
        }
116
        $options['headers'] = [
117
            'Content-Type' => 'application/json',
118
            'Authorization' => sprintf('Bearer %s', $this->bearerToken),
119
        ];
120
121
        return $options;
122
    }
123
124
    /**
125
     * @param \Exception $exception
126
     *
127
     * @return \Exception|ApiException
128
     */
129 1
    public function transformApiException(\Exception $exception)
130
    {
131 1
        return $this->apiExceptionTransformer->transform($exception);
132
    }
133
}
134