Passed
Push — master ( 22b76c...9bec39 )
by Monney
04:18
created

PinterestProvider::getAuthUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
            [
43
                'headers' => [
44
                    'Authorization' => 'Bearer '.$token,
45
                ],
46
            ]
47
        );
48
        $contents = $response->getBody()->getContents();
49
        return json_decode($contents, true);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function mapUserToObject(array $user)
56
    {
57
        preg_match('#https://www.pinterest.com/(.+?)/#', $user['data']['url'], $matches);
58
        $nickname = $matches[1];
59
        return (new User())->setRaw($user)->map(
60
            [
61
                'id'       => $user['data']['id'],
62
                'nickname' => $nickname,
63
                'name'     => $user['data']['first_name'].' '.$user['data']['last_name'],
64
            ]
65
        );
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function getTokenFields($code)
72
    {
73
        return array_merge(
74
            parent::getTokenFields($code),
75
            [
76
                'grant_type' => 'authorization_code',
77
            ]
78
        );
79
    }
80
}
81