AuthorizationEndPoint   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 26
c 1
b 0
f 0
dl 0
loc 121
ccs 28
cts 28
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A state() 0 11 2
A name() 0 3 1
A code() 0 20 4
A __construct() 0 3 1
A apply() 0 3 1
A uri() 0 3 1
A scope() 0 3 1
1
<?php
2
3
namespace Parroauth2\Client\EndPoint\Authorization;
4
5
use Base64Url\Base64Url;
6
use Parroauth2\Client\Client;
7
use Parroauth2\Client\ClientInterface;
8
use Parroauth2\Client\EndPoint\EndPointInterface;
9
use Parroauth2\Client\EndPoint\EndPointParametersTrait;
10
use Parroauth2\Client\EndPoint\EndPointTransformerInterface;
11
use Parroauth2\Client\Exception\UnsupportedServerOperation;
12
13
/**
14
 * The authorization endpoint
15
 * This endpoint must be called by the user agent using a redirection
16
 *
17
 * @see https://tools.ietf.org/html/rfc6749#section-3.1
18
 */
19
class AuthorizationEndPoint implements EndPointInterface
20
{
21
    use EndPointParametersTrait;
22
23
    public const NAME = 'authorization';
24
25
    public const RESPONSE_TYPE_CODE = 'code';
26
    public const RESPONSE_TYPE_TOKEN = 'token';
27
28
    /**
29
     * @var ClientInterface
30
     * @readonly
31
     */
32
    private $client;
33
34
    /**
35
     * AuthorizationEndPoint constructor.
36
     *
37
     * @param ClientInterface $client
38
     */
39 144
    public function __construct(ClientInterface $client)
40
    {
41 144
        $this->client = $client;
42 144
    }
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * @psalm-mutation-free
48
     */
49 144
    public function name(): string
50
    {
51 144
        return self::NAME;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     *
57
     * @return static
58
     */
59 11
    public function apply(EndPointTransformerInterface $transformer): AuthorizationEndPoint
60
    {
61 11
        return $transformer->onAuthorization($this);
62
    }
63
64
    /**
65
     * Authorization request for get an authorization code (i.e. response_type=code)
66
     *
67
     * @param string|null $redirectUri The URI to redirect after authorization
68
     * @param string[] $scopes List of scopes
69
     *
70
     * @return static
71
     *
72
     * @see https://tools.ietf.org/html/rfc6749#section-4.1.1
73
     */
74 37
    public function code(?string $redirectUri = null, array $scopes = []): AuthorizationEndPoint
75
    {
76 37
        $endpoint = clone $this;
77
78 37
        $endpoint->parameters['client_id'] = $endpoint->client->clientId();
79 37
        $endpoint->parameters['response_type'] = self::RESPONSE_TYPE_CODE;
80
81 37
        if ($redirectUri) {
82 15
            $endpoint->parameters['redirect_uri'] = $redirectUri;
83
        }
84
85 37
        if ($scopes) {
86 17
            $endpoint = $endpoint->scope($scopes);
87
        }
88
89 37
        if (empty($endpoint->parameters['state'])) {
90 37
            $endpoint = $endpoint->state();
91
        }
92
93 37
        return $endpoint;
94
    }
95
96
    /**
97
     * Define scopes
98
     *
99
     * @param string[] $scopes
100
     *
101
     * @return static
102
     *
103
     * @psalm-mutation-free
104
     */
105 26
    public function scope(array $scopes): AuthorizationEndPoint
106
    {
107 26
        return $this->set('scope', implode(' ', $scopes));
108
    }
109
110
    /**
111
     * Define a state
112
     * The state will be saved into the client session
113
     *
114
     * @param string|null $state The state, or null to generates the state
115
     *
116
     * @return static
117
     */
118 41
    public function state(?string $state = null): AuthorizationEndPoint
119
    {
120 41
        $endpoint = clone $this;
121
122 41
        if ($state === null) {
123 39
            $state = Base64Url::encode(random_bytes(32));
124
        }
125
126 41
        $endpoint->parameters['state'] = $state;
127
128 41
        return $endpoint;
129
    }
130
131
    /**
132
     * Generates the authorization URI
133
     *
134
     * @return string
135
     * @throws UnsupportedServerOperation
136
     */
137 46
    public function uri(): string
138
    {
139 46
        return $this->client->endPoints()->uri($this);
140
    }
141
}
142