Completed
Pull Request — master (#4)
by Laurens
02:47
created

OauthTokenService::refreshToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 14
nc 1
nop 4
1
<?php
2
namespace Werkspot\BingAdsApiBundle\Guzzle;
3
4
use GuzzleHttp\ClientInterface;
5
use Werkspot\BingAdsApiBundle\Model\AccessToken;
6
7
class OauthTokenService
8
{
9
    private $httpClient;
10
11
    const URL = 'https://login.live.com/oauth20_token.srf';
12
    const GRANTTYPE = 'refresh_token';
13
14
    public function __construct(ClientInterface $httpClient)
15
    {
16
        $this->httpClient = $httpClient;
17
    }
18
19
    /**
20
     * @param string $clientId
21
     * @param string $clientSecret
22
     * @param string $redirectUri
23
     * @param AccessToken $accessToken
24
     *
25
     * @return AccessToken
26
     */
27
    public function refreshToken($clientId, $clientSecret, $redirectUri, AccessToken $accessToken)
28
    {
29
        $data = [
30
            'headers' => [
31
                'Content-Type' => 'application/x-www-form-urlencoded'
32
            ],
33
            'form_params' => [
34
                'client_id' => $clientId,
35
                'client_secret' => $clientSecret,
36
                'grant_type' => self::GRANTTYPE,
37
                'redirect_uri' => $redirectUri,
38
                'refresh_token' => $accessToken->getRefreshToken(),
39
            ]
40
        ];
41
        $response = $this->httpClient->request('POST', self::URL, $data);
42
43
        $json = json_decode($response->getBody(), true);
44
        $tokens = new AccessToken($json['access_token'], $json['refresh_token']);
45
46
        return $tokens;
47
    }
48
}
49