Completed
Push — master ( 638b83...705810 )
by Alex
01:58
created

HeadHunter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 4
dl 0
loc 106
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseAuthorizationUrl() 0 4 1
A checkResponse() 0 6 2
A createResourceOwner() 0 4 1
A getDefaultScopes() 0 4 1
A getBaseAccessTokenUrl() 0 9 2
A getResourceOwnerDetailsUrl() 0 7 1
A getAuthorizationParameters() 0 15 3
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 1
        }
56
57 3
        return $this->urlAccessToken.'?'.
58 3
            $this->buildQueryString($params);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64 1
    public function getResourceOwnerDetailsUrl(AccessToken $token)
65
    {
66 1
        return $this->urlApi.'/me?'.
67 1
            $this->buildQueryString([
68 1
                'access_token' => (string) $token,
69 1
            ]);
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 1
        }
91
92 1
        if (empty($options['redirect_uri'])) {
93 1
            $options['redirect_uri'] = $this->redirectUri;
94 1
        }
95
96 1
        return $options;
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102 3
    protected function checkResponse(ResponseInterface $response, $data)
103
    {
104 3
        if (isset($data['error'])) {
105 1
            throw HeadHunterException::errorResponse($response, $data);
106
        }
107 2
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112 1
    protected function createResourceOwner(array $response, AccessToken $token)
113
    {
114 1
        return new HeadHunterResourceOwner($response);
115
    }
116
}
117