Completed
Push — master ( 414518...ca2fd8 )
by Zbigniew
02:25
created

GuzzleClient::executeRequestForParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
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 15
    public function __construct(ApiExceptionTransformerInterface $apiExceptionTransformer, array $config = [])
42
    {
43 15
        $this->apiExceptionTransformer = $apiExceptionTransformer;
44 15
        parent::__construct($config);
45 15
    }
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 12
    public function setBearerToken($bearerToken)
62
    {
63 12
        if (is_string($bearerToken) === false) {
64 1
            throw new \InvalidArgumentException('Bearer Token should be a string!');
65 1
        }
66 11
        $this->bearerToken = $bearerToken;
67
68 11
        return $this;
69
    }
70
71
    /**
72
     * @param \Exception $exception
73
     *
74
     * @return \Exception|ApiException
75
     */
76 1
    public function transformApiException(\Exception $exception)
77
    {
78 1
        return $this->apiExceptionTransformer->transform($exception);
79
    }
80
81
    /**
82
     * @param string $requestMethod
83
     * @param string $path
84
     * @param array $params
85
     *
86
     * @return ResponseInterface
87
     * @throws \InvalidArgumentException
88
     * @throws \Exception|ApiException
89
     */
90 11
    public function executeRequestForParams($requestMethod, $path, array $params)
91
    {
92 11
        $options = $this->calculateOptionsForParams($requestMethod, $params);
93
94 10
        return $this->request($requestMethod, $path, $options);
95
    }
96
97
    /**
98
     * @param string $requestMethod
99
     * @param array $params
100
     *
101
     * @return array
102
     * @throws \InvalidArgumentException
103
     */
104 11
    protected function calculateOptionsForParams($requestMethod, array $params)
105
    {
106 11
        $requestMethod = strtoupper($requestMethod);
107 11
        $options = [];
108
109
        switch ($requestMethod) {
110 11
            case RequestMethodEnum::GET:
111 4
                if (count($params) > 0) {
112 2
                    $options['query'] = $params;
113 2
                }
114 4
                break;
115 7
            case RequestMethodEnum::PUT:
116 7
            case RequestMethodEnum::POST:
117 4
                if (count($params) > 0) {
118 2
                    $options['json'] = $params;
119 2
                }
120 4
                break;
121 3
            case RequestMethodEnum::DELETE:
122 2
                break;
123 1
            default:
124 1
                throw new \InvalidArgumentException();
125 1
        }
126 10
        $options['headers']['Content-Type'] = 'application/json';
127 10
        if ($this->bearerToken !== '') {
128 2
            $options['headers']['Authorization'] = sprintf('Bearer %s', $this->bearerToken);
129 2
        }
130
131 10
        return $options;
132
    }
133
}
134