Passed
Push — main ( d40f42...a0cbf5 )
by Breno
02:18
created

GovBr   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 26
c 3
b 0
f 0
dl 0
loc 73
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A productionEnvironment() 0 3 1
A getNonce() 0 3 1
A __construct() 0 4 1
A getAuthorizationUrl() 0 7 2
A stagingEnvironment() 0 3 1
A getDefaultScopes() 0 8 1
A getScopeSeparator() 0 3 1
A createResourceOwner() 0 3 1
A getAvatar() 0 14 1
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\OAuth2\Client;
5
6
use League\OAuth2\Client\Provider\GenericProvider;
7
use League\OAuth2\Client\Token\AccessToken;
8
9
final class GovBr extends GenericProvider
10
{
11
    public function __construct(array $options = [], array $collaborators = [])
12
    {
13
        $options = array_merge($options, Environment::production());
14
        parent::__construct($options, $collaborators);
15
    }
16
17
    public static function stagingEnvironment(array $options): self
18
    {
19
        return new self(array_merge($options, Environment::staging()));
20
    }
21
22
    public static function productionEnvironment(array $options): self
23
    {
24
        return new self(array_merge($options, Environment::production()));
25
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function getAuthorizationUrl(array $options = []): string
31
    {
32
        if (!isset($options['nonce'])) {
33
            $options['nonce'] = $this->getNonce();
34
        }
35
36
        return parent::getAuthorizationUrl($options);
37
    }
38
39
    private function getNonce(): string
40
    {
41
        return md5(uniqid('govbr', true));
42
    }
43
44
    public function getDefaultScopes(): array
45
    {
46
        return [
47
            'openid',
48
            'email',
49
            'phone',
50
            'profile',
51
            'govbr_confiabilidades'
52
        ];
53
    }
54
55
    public function getScopeSeparator(): string
56
    {
57
        return '+';
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    protected function createResourceOwner(array $response, AccessToken $token)
64
    {
65
        return new GovBrUser($response, $token);
66
    }
67
68
    public function getAvatar(GovBrUser $govBrUser): ?Avatar
69
    {
70
        $request = $this->getAuthenticatedRequest(
71
            self::METHOD_GET,
72
            $govBrUser->getAvatarUrl(),
73
            $govBrUser->token()
74
        );
75
76
        $response = $this->getResponse($request);
77
78
        return
79
            new Avatar(
80
                (string) $response->getBody(),
81
                $response->getHeaderLine('Content-type')
82
            );
83
    }
84
}
85