Passed
Push — master ( c84844...9acf48 )
by Zbigniew
02:35
created

GuzzleClient::getResponseFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the zibios/wrike-php-guzzle package.
5
 *
6
 * (c) Zbigniew Ślązak
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zibios\WrikePhpGuzzle\Client;
13
14
use GuzzleHttp\Client as BaseClient;
15
use Psr\Http\Message\ResponseInterface;
16
use Zibios\WrikePhpLibrary\Api;
17
use Zibios\WrikePhpLibrary\Client\ClientInterface;
18
use Zibios\WrikePhpLibrary\Enum\Api\RequestMethodEnum;
19
use Zibios\WrikePhpLibrary\Enum\Api\ResponseFormatEnum;
20
use Zibios\WrikePhpLibrary\Validator\AccessTokenValidator;
21
22
/**
23
 * Guzzle Client for Wrike library.
24
 */
25
class GuzzleClient extends BaseClient implements ClientInterface
26
{
27
    /**
28
     * @return string
29
     */
30 1
    public function getResponseFormat()
31
    {
32 1
        return ResponseFormatEnum::PSR_RESPONSE;
33
    }
34
35
    /**
36
     * Request method.
37
     *
38
     * Generic format for HTTP client request method.
39
     *
40
     * @param string $requestMethod GT/POST/PUT/DELETE
41
     * @param string $path          full path to REST resource without domain, ex. 'accounts/XXXXXXXX/contacts'
42
     * @param array  $params        optional params for GET/POST request
43
     * @param string $accessToken   Access Token for Wrike access
44
     *
45
     * @see \Zibios\WrikePhpLibrary\Enum\Api\RequestMethodEnum
46
     * @see \Zibios\WrikePhpLibrary\Enum\Api\RequestPathFormatEnum
47
     *
48
     * @throws \Exception
49
     * @throws \InvalidArgumentException
50
     *
51
     * @return string|ResponseInterface
52
     */
53 13
    public function executeRequestForParams($requestMethod, $path, array $params, $accessToken)
54
    {
55 13
        RequestMethodEnum::assertIsValidValue($requestMethod);
56 12
        $options = $this->calculateOptionsForParams($requestMethod, $params, $accessToken);
57 10
        if ($requestMethod === RequestMethodEnum::UPLOAD) {
58 2
            $requestMethod = RequestMethodEnum::POST;
59
        }
60
61 10
        return $this->request($requestMethod, $path, $options);
62
    }
63
64
    /**
65
     * Main method for calculating request params.
66
     *
67
     * @param string $requestMethod
68
     * @param array  $params
69
     * @param $accessToken
70
     *
71
     * @throws \InvalidArgumentException
72
     *
73
     * @return array
74
     */
75 12
    protected function calculateOptionsForParams($requestMethod, array $params, $accessToken)
76
    {
77 12
        $options = $this->prepareBaseOptions($accessToken);
78 10
        if (count($params) === 0) {
79 5
            return $options;
80
        }
81
82
        switch ($requestMethod) {
83 5
            case RequestMethodEnum::GET:
84 4
            case RequestMethodEnum::DELETE:
85 2
                if (count($params) > 0) {
86 2
                    $options['query'] = $params;
87
                }
88 2
                break;
89 3
            case RequestMethodEnum::PUT:
90 2
            case RequestMethodEnum::POST:
91 2
                if (count($params) > 0) {
92 2
                    $options['form_params'] = $params;
93
                }
94 2
                break;
95 1
            case RequestMethodEnum::UPLOAD:
96 1
                if (array_key_exists('resource', $params) && array_key_exists('name', $params)) {
97 1
                    $options['headers']['X-File-Name'] = $params['name'];
98 1
                    $options['multipart'] = [
99
                        [
100 1
                            'contents' => $params['resource'],
101 1
                            'name' => $params['name'],
102
                        ],
103
                    ];
104
                }
105 1
                break;
106
            default:
107
                throw new \InvalidArgumentException();
108
        }
109
110 5
        return $options;
111
    }
112
113
    /**
114
     * @param $accessToken
115
     *
116
     * @throws \InvalidArgumentException
117
     *
118
     * @return array
119
     */
120 12
    protected function prepareBaseOptions($accessToken)
121
    {
122 12
        AccessTokenValidator::assertIsValid($accessToken);
123 10
        $options = [];
124 10
        $options['headers']['Authorization'] = sprintf('Bearer %s', $accessToken);
125 10
        $options['base_uri'] = Api::BASE_URI;
126
127 10
        return $options;
128
    }
129
}
130