Completed
Push — master ( 6ad9e3...7a68b3 )
by Dmitry
01:36
created

AbstractService::prepareGetParams()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 15
rs 10
cc 4
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Promopult\TikTokMarketingApi;
6
7
abstract class AbstractService implements \Promopult\TikTokMarketingApi\ServiceInterface
8
{
9
    /**
10
     * @var CredentialsInterface
11
     */
12
    protected $credentials;
13
14
    /**
15
     * @var \Psr\Http\Client\ClientInterface
16
     */
17
    protected $httpClient;
18
19
    public function __construct(
20
        \Promopult\TikTokMarketingApi\CredentialsInterface $credentials,
21
        \Psr\Http\Client\ClientInterface $httpClient
22
    ) {
23
        $this->credentials = $credentials;
24
        $this->httpClient = $httpClient;
25
    }
26
27
    public function requestApi(
28
        string $httpMethod,
29
        string $endpoint,
30
        array $args = []
31
    ): array {
32
        $httpMethod = strtolower($httpMethod);
33
34
        $args = array_filter($args);
35
36
        $url = $this->credentials->getApiBaseUrl() . $endpoint;
37
38
        if ($args && $httpMethod === 'get') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39
            $url .= '?' . $this->prepareGetParams($args);
40
        }
41
42
        $headers = [
43
            'Content-Type' => 'application/json',
44
            'Accept' => 'application/json',
45
            'Access-Token' => $this->credentials->getAccessToken(),
46
        ];
47
48
        $body = $httpMethod === 'get' || empty($args)
49
            ? null
50
            : \json_encode($args);
51
52
        $request = new \GuzzleHttp\Psr7\Request(
53
            $httpMethod,
54
            $url,
55
            $headers,
56
            $body
57
        );
58
59
        $response = $this->httpClient->sendRequest($request);
60
61
        return $this->handleResponse($response, $request);
62
    }
63
64
    protected function handleResponse(
65
        \Psr\Http\Message\ResponseInterface $response,
66
        \Psr\Http\Message\RequestInterface $request
67
    ): array {
68
        $decodedJson = \json_decode($response->getBody()->getContents(), true);
69
70
        if (empty($decodedJson)) {
71
            throw new \Promopult\TikTokMarketingApi\Exception\UnexpectedApiResponse(
72
                $request,
73
                $response
74
            );
75
        }
76
77
        return $decodedJson;
78
    }
79
80
    protected function prepareGetParams(array $args): string
81
    {
82
        $formedArgs = [];
83
84
        foreach ($args as $arg => $value) {
85
            if (is_scalar($value)) {
86
                $formedArgs[$arg] = $value;
87
            }
88
89
            if (is_array($value)) {
90
                $formedArgs[$arg] = \json_encode(array_filter($value));
91
            }
92
        }
93
94
        return http_build_query($formedArgs);
95
    }
96
}
97