PinterestProvider::getTokenFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Mckenziearts\LaravelOAuth\Providers;
4
5
use Laravel\Socialite\Two\User;
6
use Laravel\Socialite\Two\AbstractProvider;
7
use Laravel\Socialite\Two\ProviderInterface;
8
9
class PinterestProvider extends AbstractProvider implements ProviderInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    protected $scopes = ['read_public'];
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected function getAuthUrl($state)
20
    {
21
        return $this->buildAuthUrlFromBase(
22
            'https://api.pinterest.com/oauth/',
23
            $state
24
        );
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function getTokenUrl()
31
    {
32
        return 'https://api.pinterest.com/v1/oauth/token';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function getUserByToken($token)
39
    {
40
        $response = $this->getHttpClient()->get(
41
            'https://api.pinterest.com/v1/me/',
42
            ['headers' => ['Authorization' => 'Bearer '.$token]]
43
        );
44
        $contents = $response->getBody()->getContents();
45
46
        return json_decode($contents, true);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function mapUserToObject(array $user)
53
    {
54
        preg_match('#https://www.pinterest.com/(.+?)/#', $user['data']['url'], $matches);
55
        $nickname = $matches[1];
56
57
        return (new User())->setRaw($user)->map(
58
            [
59
                'id'       => $user['data']['id'],
60
                'nickname' => $nickname,
61
                'name'     => $user['data']['first_name'].' '.$user['data']['last_name'],
62
            ]
63
        );
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function getTokenFields($code)
70
    {
71
        return array_merge(
72
            parent::getTokenFields($code), ['grant_type' => 'authorization_code']
73
        );
74
    }
75
}
76