Auth   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
eloc 31
c 3
b 0
f 0
dl 0
loc 132
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getAppId() 0 3 1
A setSecret() 0 3 1
A __construct() 0 5 1
A setAppId() 0 3 1
A getSecret() 0 3 1
A httpPostJson() 0 17 5
A refreshTokens() 0 9 1
A getTokens() 0 9 1
1
<?php
2
3
namespace CloudyCity\KuaishouMarketingSDK;
4
5
use CloudyCity\KuaishouMarketingSDK\Kernel\Exceptions\AuthException;
6
use CloudyCity\KuaishouMarketingSDK\Kernel\Traits\HasHttpRequests;
7
8
class Auth
9
{
10
    use HasHttpRequests;
11
12
    /**
13
     * @var string
14
     */
15
    protected $appId;
16
17
    /**
18
     * @var string
19
     */
20
    protected $secret;
21
22
    /**
23
     * @var string
24
     */
25
    protected $baseUri = 'https://ad.e.kuaishou.com/rest/openapi/';
26
27
    /**
28
     * Auth constructor.
29
     *
30
     * @param $appId
31
     * @param $secret
32
     * @param $responseType
33
     */
34
    public function __construct($appId, $secret, $responseType = 'array')
35
    {
36
        $this->setAppId($appId);
37
        $this->setSecret($secret);
38
        $this->setResponseType($responseType);
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getAppId()
45
    {
46
        return $this->appId;
47
    }
48
49
    /**
50
     * @param string $appId
51
     */
52
    public function setAppId($appId)
53
    {
54
        $this->appId = $appId;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getSecret()
61
    {
62
        return $this->secret;
63
    }
64
65
    /**
66
     * @param string $secret
67
     */
68
    public function setSecret($secret)
69
    {
70
        $this->secret = $secret;
71
    }
72
73
    /**
74
     * @param $authCode
75
     *
76
     * @throws AuthException
77
     * @throws \GuzzleHttp\Exception\GuzzleException
78
     * @throws Kernel\Exceptions\InvalidArgumentException
79
     *
80
     * @return array|Kernel\Http\Response|Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface
81
     */
82
    public function getTokens($authCode)
83
    {
84
        $params = [
85
            'app_id'    => $this->getAppId(),
86
            'secret'    => $this->getSecret(),
87
            'auth_code' => $authCode,
88
        ];
89
90
        return $this->httpPostJson('oauth2/authorize/access_token', $params);
91
    }
92
93
    /**
94
     * @param $refreshToken
95
     *
96
     * @throws AuthException
97
     * @throws Kernel\Exceptions\InvalidArgumentException
98
     * @throws \GuzzleHttp\Exception\GuzzleException
99
     *
100
     * @return array|Kernel\Http\Response|Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface
101
     */
102
    public function refreshTokens($refreshToken)
103
    {
104
        $params = [
105
            'app_id'        => $this->getAppId(),
106
            'secret'        => $this->getSecret(),
107
            'refresh_token' => $refreshToken,
108
        ];
109
110
        return $this->httpPostJson('oauth2/authorize/refresh_token', $params);
111
    }
112
113
    /**
114
     * @param $url
115
     * @param array $params
116
     *
117
     * @throws AuthException
118
     * @throws Kernel\Exceptions\InvalidArgumentException
119
     * @throws \GuzzleHttp\Exception\GuzzleException
120
     *
121
     * @return array|Kernel\Http\Response|Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface
122
     */
123
    private function httpPostJson($url, array $params)
124
    {
125
        $response = $this->request($url, 'POST', [
126
            'json' => $params,
127
        ]);
128
129
        $result = $this->castResponseToType($response);
130
        $formatted = $this->castResponseToType($response, $this->getResponseType());
131
132
        if (!isset($result['code']) || $result['code'] != 0) {
133
            $message = isset($result['message']) ? $result['message'] : '';
134
            $code = isset($result['code']) ? $result['code'] : 0;
135
136
            throw new AuthException($message, $response, $formatted, $code);
137
        }
138
139
        return $formatted;
140
    }
141
}
142