Completed
Push — master ( 374db5...638b83 )
by Alex
02:01
created

HeadHunter::getBaseAccessTokenUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace AlexMasterov\OAuth2\Client\Provider;
4
5
use AlexMasterov\OAuth2\Client\Provider\Exception\HeadHunterException;
6
use League\OAuth2\Client\Provider\AbstractProvider;
7
use League\OAuth2\Client\Token\AccessToken;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use Psr\Http\Message\ResponseInterface;
10
11
class HeadHunter extends AbstractProvider
12
{
13
    use BearerAuthorizationTrait;
14
15
    /**
16
     * @var string
17
     */
18
    protected $urlApi = 'https://api.hh.ru';
19
20
    /**
21
     * @var string
22
     */
23
    protected $urlAuthorize = 'https://hh.ru/oauth/authorize';
24
25
    /**
26
     * @var string
27
     */
28
    protected $urlAccessToken = 'https://hh.ru/oauth/token';
29
30
    /**
31
     * @var string
32
     */
33
    protected $state;
34
35
    /**
36
     * @var string
37
     */
38
    protected $redirectUri;
39
40
    /**
41
     * @inheritDoc
42
     */
43 1
    public function getBaseAuthorizationUrl()
44
    {
45 1
        return $this->urlAuthorize;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 3
    public function getBaseAccessTokenUrl(array $params)
52
    {
53 3
        if (empty($params['code'])) {
54 1
            $params['code'] = '';
55
        }
56
57 3
        return $this->urlAccessToken.'?'.
58 3
            $this->buildQueryString($params);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function getResourceOwnerDetailsUrl(AccessToken $token)
65
    {
66
        return $this->urlApi.'/me?'.
67
            $this->buildQueryString([
68
                'access_token' => (string) $token,
69
            ]);
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 1
    protected function getDefaultScopes()
76
    {
77 1
        return [];
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83 1
    protected function getAuthorizationParameters(array $options)
84
    {
85 1
        $options['response_type'] = 'code';
86 1
        $options['client_id'] = $this->clientId;
87
88 1
        if (empty($options['state'])) {
89 1
            $options['state'] = $this->state;
90
        }
91
92 1
        if (empty($options['redirect_uri'])) {
93 1
            $options['redirect_uri'] = $this->redirectUri;
94
        }
95
96 1
        return $options;
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102 2
    protected function checkResponse(ResponseInterface $response, $data)
103
    {
104 2
        if (isset($data['error'])) {
105 1
            throw HeadHunterException::errorResponse($response, $data);
106
        }
107 1
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    protected function createResourceOwner(array $response, AccessToken $token)
113
    {
114
        return new HeadHunterResourceOwner($response);
115
    }
116
}
117