Passed
Push — master ( a782e3...c066f8 )
by CloudyCity
01:31
created

Auth::getBaseUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace CloudyCity\TencentMarketingSDK;
4
5
use CloudyCity\TencentMarketingSDK\Kernel\Exceptions\AuthException;
6
use CloudyCity\TencentMarketingSDK\Kernel\Traits\HasHttpRequests;
7
8
class Auth
9
{
10
    use HasHttpRequests;
11
12
    /**
13
     * @var string
14
     */
15
    protected $clientId;
16
17
    /**
18
     * @var string
19
     */
20
    protected $clientSecret;
21
22
    /**
23
     * @var string
24
     */
25
    protected $baseUrl = 'https://api.e.qq.com/';
26
27
    /**
28
     * Auth constructor.
29
     *
30
     * @param $clientId
31
     * @param $clientSecret
32
     * @param $responseType
33
     */
34
    public function __construct($clientId, $clientSecret, $responseType = 'array')
35
    {
36
        $this->setClientId($clientId);
37
        $this->setClientSecret($clientSecret);
38
        $this->setResponseType($responseType);
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getBaseUrl()
45
    {
46
        return $this->baseUrl;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getClientId()
53
    {
54
        return $this->clientId;
55
    }
56
57
    /**
58
     * @param string $clientId
59
     */
60
    public function setClientId($clientId)
61
    {
62
        $this->clientId = $clientId;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getClientSecret()
69
    {
70
        return $this->clientSecret;
71
    }
72
73
    /**
74
     * @param string $clientSecret
75
     */
76
    public function setClientSecret($clientSecret)
77
    {
78
        $this->clientSecret = $clientSecret;
79
    }
80
81
    /**
82
     * @param $authCode
83
     * @param $redirectUri
84
     *
85
     * @throws AuthException
86
     * @throws Kernel\Exceptions\Exception
87
     * @throws Kernel\Exceptions\InvalidArgumentException
88
     *
89
     * @return array|Kernel\Http\Response|\Doctrine\Common\Collections\ArrayCollection|object|\Psr\Http\Message\ResponseInterface
90
     */
91
    public function getTokens($authCode, $redirectUri)
92
    {
93
        $params = [
94
            'client_id'          => $this->getClientId(),
95
            'client_secret'      => $this->getClientSecret(),
96
            'grant_type'         => 'authorization_code',
97
            'authorization_code' => $authCode,
98
            'redirect_uri'       => $redirectUri,
99
        ];
100
101
        return $this->get('oauth/token', $params);
102
    }
103
104
    /**
105
     * @param $refreshToken
106
     *
107
     * @throws AuthException
108
     * @throws Kernel\Exceptions\Exception
109
     * @throws Kernel\Exceptions\InvalidArgumentException
110
     *
111
     * @return array|Kernel\Http\Response|\Doctrine\Common\Collections\ArrayCollection|object|\Psr\Http\Message\ResponseInterface
112
     */
113
    public function refreshTokens($refreshToken)
114
    {
115
        $params = [
116
            'client_id'     => $this->getClientId(),
117
            'client_secret' => $this->getClientSecret(),
118
            'grant_type'    => 'refresh_token',
119
            'refresh_token' => $refreshToken,
120
        ];
121
122
        return $this->get('oauth/token', $params);
123
    }
124
125
    /**
126
     * @param $url
127
     * @param array $params
128
     *
129
     * @throws AuthException
130
     * @throws Kernel\Exceptions\Exception
131
     * @throws Kernel\Exceptions\InvalidArgumentException
132
     *
133
     * @return array|Kernel\Http\Response|\Doctrine\Common\Collections\ArrayCollection|object|\Psr\Http\Message\ResponseInterface
134
     */
135
    private function get($url, array $params)
136
    {
137
        $response = $this->request($url, 'GET', [
138
            'form_params' => $params,
139
        ]);
140
141
        $result = $this->castResponseToType($response);
142
        $formatted = $this->castResponseToType($response, $this->getResponseType());
143
144
        if (!isset($result['code']) || $result['code'] != 0) {
145
            $message = isset($result['message']) ? $result['message'] : '';
146
            $code = isset($result['code']) ? $result['code'] : 0;
147
148
            throw new AuthException($message, $response, $formatted, $code);
149
        }
150
151
        return $formatted;
152
    }
153
}
154