Test Failed
Push — master ( 707b72...8fb8e3 )
by Zbigniew
02:45
created

GuzzleClient   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 90
ccs 24
cts 27
cp 0.8889
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseFormat() 0 4 1
A executeRequestForParams() 0 8 1
C calculateOptionsForParams() 0 24 7
A prepareBaseOptions() 0 9 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
    public function getResponseFormat()
31
    {
32
        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 11
    public function executeRequestForParams($requestMethod, $path, array $params, $accessToken)
54
    {
55 11
        RequestMethodEnum::assertIsValidValue($requestMethod);
56
57 10
        $options = $this->calculateOptionsForParams($requestMethod, $params, $accessToken);
58
59 8
        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 $accessToken
68
     *
69
     * @throws \InvalidArgumentException
70
     *
71
     * @return array
72
     */
73 10
    protected function calculateOptionsForParams($requestMethod, array $params, $accessToken)
74
    {
75 10
        $options = $this->prepareBaseOptions($accessToken);
76 8
        if (count($params) === 0) {
77 4
            return $options;
78
        }
79
80
        switch ($requestMethod) {
81 4
            case RequestMethodEnum::GET:
82 3
            case RequestMethodEnum::DELETE:
83 2
                $options['query'] = $params;
84 2
                break;
85 2
            case RequestMethodEnum::PUT:
86 1
            case RequestMethodEnum::POST:
87 2
                if (count($params) > 0) {
88 2
                    $options['form_params'] = $params;
89
                }
90 2
                break;
91
            default:
92
                throw new \InvalidArgumentException();
93
        }
94
95 4
        return $options;
96
    }
97
98
    /**
99
     * @param $accessToken
100
     *
101
     * @throws \InvalidArgumentException
102
     *
103
     * @return array
104
     */
105 10
    protected function prepareBaseOptions($accessToken)
106
    {
107 10
        AccessTokenValidator::assertIsValid($accessToken);
108 8
        $options = [];
109 8
        $options['headers']['Authorization'] = sprintf('Bearer %s', $accessToken);
110 8
        $options['base_uri'] = Api::BASE_URI;
111
112 8
        return $options;
113
    }
114
}
115