Custom   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseAuthorizationUrl() 0 3 1
A getResourceOwnerDetailsUrl() 0 3 1
A getBaseAccessTokenUrl() 0 3 1
A getDefaultScopes() 0 3 1
A checkResponse() 0 8 3
A __construct() 0 17 5
A createResourceOwner() 0 5 1
A getTokenData() 0 9 1
1
<?php
2
3
namespace Sludio\HelperBundle\Oauth\Client\Provider\Custom;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Token\AccessToken;
7
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
8
use Psr\Http\Message\ResponseInterface;
9
use Sludio\HelperBundle\Oauth\Exception\CustomIdentityProviderException;
10
11
class Custom extends AbstractProvider
12
{
13
    use BearerAuthorizationTrait;
14
15
    public $domain;
16
    public $api;
17
    public $authorize;
18
    public $token;
19
20
    public $options;
21
22
    public function __construct(array $options, array $collaborators = [])
23
    {
24
        if (isset($options['domain'])) {
25
            $this->domain = $options['domain'];
26
        }
27
        if (isset($options['api'])) {
28
            $this->api = $options['api'];
29
        }
30
        if (isset($options['authorize'])) {
31
            $this->authorize = $options['authorize'];
32
        }
33
        if (isset($options['token'])) {
34
            $this->token = $options['token'];
35
        }
36
37
        $this->options = $options;
38
        parent::__construct($options, $collaborators);
39
    }
40
41
    public function getBaseAuthorizationUrl()
42
    {
43
        return $this->domain.$this->authorize;
44
    }
45
46
    public function getBaseAccessTokenUrl(array $params)
47
    {
48
        return $this->domain.$this->token.'?client_id='.$this->options['client_id'].'&client_secret='.$this->options['client_secret'];
49
    }
50
51
    public function getTokenData()
52
    {
53
        $data = [
54
            'url' => $this->domain.$this->token,
55
            'client_id' => $this->options['client_id'],
56
            'client_secret' => $this->options['client_secret'],
57
        ];
58
59
        return $data;
60
    }
61
62
    public function getResourceOwnerDetailsUrl(AccessToken $token)
63
    {
64
        return $this->domain.$this->api;
65
    }
66
67
    protected function getDefaultScopes()
68
    {
69
        return [];
70
    }
71
72
    protected function checkResponse(ResponseInterface $response, $data)
73
    {
74
        if ($response->getStatusCode() >= 400) {
75
            throw CustomIdentityProviderException::clientException($response, $data);
76
        }
77
78
        if (isset($data['error'])) {
79
            throw CustomIdentityProviderException::oauthException($response, $data);
80
        }
81
    }
82
83
    protected function createResourceOwner(array $response, AccessToken $token)
84
    {
85
        $user = new CustomResourceOwner($response, 0);
86
87
        return $user->setDomain($this->domain);
88
    }
89
}
90