BaseClient   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 141
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A httpPostJson() 0 4 1
A httpPostFile() 0 4 1
B request() 0 24 7
A appendToken() 0 19 3
A isDownloadFile() 0 4 1
1
<?php
2
3
namespace CloudyCity\UCMarketingSDK\Kernel;
4
5
use CloudyCity\UCMarketingSDK\Kernel\Exceptions\ApiException;
6
use CloudyCity\UCMarketingSDK\Kernel\Exceptions\DownloadFileException;
7
use CloudyCity\UCMarketingSDK\Kernel\Traits\HasHttpRequests;
8
use CloudyCity\UCMarketingSDK\Kernel\Traits\HasSdkBaseInfo;
9
use Doctrine\Common\Collections\ArrayCollection;
10
11
/**
12
 * Class Client.
13
 */
14
class BaseClient
15
{
16
    use HasHttpRequests, HasSdkBaseInfo {
17
        request as performRequest;
18
        HasHttpRequests::getResponseType insteadof HasSdkBaseInfo;
19
        HasHttpRequests::setResponseType insteadof HasSdkBaseInfo;
20
    }
21
22
    /**
23
     * @var string
24
     */
25
    protected $baseUri = 'https://e.uc.cn/api/';
26
27
    /**
28
     * Client constructor.
29
     *
30
     * @param string $username
31
     * @param string $password
32
     * @param string $token
33
     * @param string $responseType
34
     */
35
    public function __construct($username, $password, $token, $responseType = 'array')
36
    {
37
        $this->setUsername($username);
38
        $this->setPassword($password);
39
        $this->setToken($token);
40
        $this->setResponseType($responseType);
41
    }
42
43
    /**
44
     * Sending json.
45
     *
46
     * @param string $url
47
     * @param array  $data
48
     * @param array  $query
49
     *
50
     * @throws Exceptions\InvalidArgumentException
51
     * @throws \GuzzleHttp\Exception\GuzzleException
52
     * @throws ApiException
53
     *
54
     * @return \Psr\Http\Message\ResponseInterface|\Doctrine\Common\Collections\ArrayCollection|array|object|string
55
     */
56
    public function httpPostJson($url, array $data = [], array $query = [])
57
    {
58
        return $this->request($url, 'POST', ['query' => $query, 'json' => $data]);
59
    }
60
61
    /**
62
     * Sending form files.
63
     *
64
     * @see http://docs.guzzlephp.org/en/stable/quickstart.html#sending-form-files
65
     *
66
     * @param $url
67
     * @param array $files
68
     *
69
     * @throws ApiException
70
     * @throws Exceptions\InvalidArgumentException
71
     * @throws \GuzzleHttp\Exception\GuzzleException
72
     *
73
     * @return array|ArrayCollection|object|\Psr\Http\Message\ResponseInterface|string
74
     */
75
    public function httpPostFile($url, array $files)
76
    {
77
        return $this->request($url, 'POST', ['multipart' => $files]);
78
    }
79
80
    /**
81
     * Sending request.
82
     *
83
     * @param string $url
84
     * @param string $method
85
     * @param array  $options
86
     *
87
     * @throws Exceptions\InvalidArgumentException
88
     * @throws Exceptions\ApiException
89
     * @throws \GuzzleHttp\Exception\GuzzleException
90
     *
91
     * @return \Psr\Http\Message\ResponseInterface|\Doctrine\Common\Collections\ArrayCollection|array|object|string
92
     */
93
    public function request($url, $method = 'POST', array $options = [])
94
    {
95
        $options = $this->appendToken($options);
96
97
        $response = $this->performRequest($url, $method, $options);
98
99
        $result = $this->castResponseToType($response);
100
        $formatted = $this->castResponseToType($response, $this->getResponseType());
101
102
        if ($this->isDownloadFile($url)) {
103
            if (!$response->getBody()->getContents()) {
104
                throw new DownloadFileException('', $response, $formatted, 0);
105
            }
106
        } else {
107
            if ((!isset($result['header']['status']) || $result['header']['status'] != 0)) {
108
                $message = isset($result['message']) ? $result['message'] : '';
109
                $code = isset($result['code']) ? $result['code'] : 0;
110
111
                throw new ApiException($message, $response, $formatted, $code);
112
            }
113
        }
114
115
        return $formatted;
116
    }
117
118
    /**
119
     * Attache token to request.
120
     *
121
     * @param array $options
122
     *
123
     * @return array
124
     */
125
    protected function appendToken(array $options)
126
    {
127
        $authorization = $this->getAuthorization();
128
129
        if (!empty($options['multipart'])) {
130
            $options['multipart']['header'] = json_encode($authorization);
131
        } else {
132
            if (empty($options['json'])) {
133
                $options['json']['header'] = $authorization;
134
            } else {
135
                $options['json'] = [
136
                    'header' => $authorization,
137
                    'body'   => $options['json'],
138
                ];
139
            }
140
        }
141
142
        return $options;
143
    }
144
145
    /**
146
     * @param $url
147
     *
148
     * @return bool
149
     */
150
    protected function isDownloadFile($url)
151
    {
152
        return $url == 'report/downloadFile';
153
    }
154
}
155