OAuth2Client::requestAccessToken()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
/**
3
 * sources.
4
 * Date: 14/08/15
5
 */
6
7
namespace Mailxpert\Authentication;
8
9
use Mailxpert\Exceptions\MailxpertSDKException;
10
use Mailxpert\MailxpertApp;
11
use Mailxpert\MailxpertClient;
12
use Mailxpert\MailxpertRequest;
13
14
/**
15
 * Class OAuth2Client
16
 * @package Mailxpert\Authentication
17
 */
18
class OAuth2Client
19
{
20
    const BASE_AUTHORIZATION_URL = 'https://app.mailxpert.ch';
21
22
    const REFRESH_TOKEN_VALIDITY = 31536000; // 365 days
23
24
    /**
25
     * @var MailxpertApp
26
     */
27
    private $app;
28
29
    /**
30
     * @var MailxpertClient
31
     */
32
    private $client;
33
34
    /**
35
     * @var
36
     */
37
    private $baseAuthorizationUrl;
38
39
    /**
40
     * @var MailxpertRequest
41
     */
42
    private $lastRequest;
43
44
    /**
45
     * OAuth2Client constructor.
46
     *
47
     * @param MailxpertApp    $mailxpertApp
48
     * @param MailxpertClient $mailxpertClient
49
     * @param string          $baseAuthorizationUrl
50
     */
51
    public function __construct(MailxpertApp $mailxpertApp, MailxpertClient $mailxpertClient, $baseAuthorizationUrl = null)
52
    {
53
        $this->app = $mailxpertApp;
54
        $this->client = $mailxpertClient;
55
        if (is_null($baseAuthorizationUrl)) {
56
            $baseAuthorizationUrl = static::BASE_AUTHORIZATION_URL;
57
        }
58
        $this->baseAuthorizationUrl = $baseAuthorizationUrl;
59
    }
60
61
    /**
62
     * @param string $redirectUrl
63
     * @param string $state
64
     * @param array  $scope
65
     * @param array  $params
66
     * @param string $separator
67
     *
68
     * @return string
69
     */
70
    public function getAuthorizationUrl($redirectUrl, $state, $scope, $params, $separator)
71
    {
72
        $params += [
73
            'client_id' => $this->app->getId(),
74
            'response_type' => 'code',
75
            'redirect_uri' => $redirectUrl,
76
            'scope' => implode(',', $scope),
77
        ];
78
79
        if ($state != null) {
80
            $params['state'] = $state;
81
        }
82
83
        return $this->baseAuthorizationUrl.'/oauth/v2/auth?'.http_build_query($params, null, $separator);
84
    }
85
86
    /**
87
     * @param string $code
88
     * @param string $redirectUrl
89
     *
90
     * @return AccessToken
91
     * @throws MailxpertSDKException
92
     */
93
    public function getAccessTokenFromCode($code, $redirectUrl)
94
    {
95
        $params = [
96
            'code' => $code,
97
            'redirect_uri' => $redirectUrl,
98
            'grant_type' => 'authorization_code',
99
        ];
100
101
        return $this->requestAccessToken($params);
102
    }
103
104
105
    /**
106
     * @param AccessToken $accessToken
107
     * @param string      $redirectUrl
108
     *
109
     * @return AccessToken
110
     * @throws MailxpertSDKException
111
     */
112
    public function getAccessTokenFromAccessToken(AccessToken $accessToken, $redirectUrl)
113
    {
114
        $params = [
115
            'refresh_token' => $accessToken->getRefreshToken(),
116
            'redirect_uri' => $redirectUrl,
117
            'grant_type' => 'refresh_token',
118
        ];
119
120
        return $this->requestAccessToken($params);
121
    }
122
123
    /**
124
     * @param array $params
125
     *
126
     * @return AccessToken
127
     * @throws MailxpertSDKException
128
     */
129
    protected function requestAccessToken(array $params)
130
    {
131
        $response = $this->sendRequestWithClientsParams('/oauth/v2/token', $params);
132
133
        $data = $response->getDecodedBody();
134
135
        if (!isset($data['access_token'])) {
136
            throw new MailxpertSDKException('Access token was not returned.', 401);
137
        }
138
139
        $expiresAt = 0;
140
        if (isset($data['expires'])) {
141
            $expiresAt = time() + $data['expires'];
142
        } elseif (isset($data['expires_in'])) {
143
            $expiresAt = time() + $data['expires_in'];
144
        }
145
146
        $refreshTokenExpireAt = time() + static::REFRESH_TOKEN_VALIDITY;
147
148
        return new AccessToken(
149
            $data['access_token'],
150
            $data['refresh_token'],
151
            $expiresAt,
152
            $data['scope'],
153
            $refreshTokenExpireAt
154
        );
155
    }
156
157
    /**
158
     * @param $endpoint
159
     * @param $params
160
     *
161
     * @return \Mailxpert\MailxpertResponse
162
     */
163
    private function sendRequestWithClientsParams($endpoint, $params)
164
    {
165
        $params += $this->getClientParams();
166
167
        $this->lastRequest = new MailxpertRequest($this->app, null, 'GET', $this->baseAuthorizationUrl.$endpoint, $params, null);
168
169
        return $this->client->sendRequest($this->lastRequest);
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    private function getClientParams()
176
    {
177
        return [
178
            'client_id' => $this->app->getId(),
179
            'client_secret' => $this->app->getSecret(),
180
        ];
181
    }
182
}
183