GuzzleClient::prepareBaseOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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