Failed Conditions
Push — ng ( f9780e...ccd5de )
by Florent
11:07
created

getCallableAfterAccessTokenIssuance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Server\TokenEndpoint\Extension;
15
16
use OAuth2Framework\Component\Server\Core\AccessToken\AccessToken;
17
use OAuth2Framework\Component\Server\Core\Client\Client;
18
use OAuth2Framework\Component\Server\Core\ResourceOwner\ResourceOwner;
19
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception;
20
use OAuth2Framework\Component\Server\TokenEndpoint\GrantType;
21
use OAuth2Framework\Component\Server\TokenEndpoint\GrantTypeData;
22
use Psr\Http\Message\ServerRequestInterface;
23
24
final class TokenEndpointExtensionManager
25
{
26
    /**
27
     * @var TokenEndpointExtension[]
28
     */
29
    private $extensions = [];
30
31
    /**
32
     * @param TokenEndpointExtension $accessTokenParameterExtension
33
     *
34
     * @return TokenEndpointExtensionManager
35
     */
36
    public function add(TokenEndpointExtension $accessTokenParameterExtension): self
37
    {
38
        $this->extensions[] = $accessTokenParameterExtension;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param ServerRequestInterface $request
45
     * @param GrantTypeData          $grantTypeData
46
     * @param GrantType              $grantType
47
     *
48
     * @return GrantTypeData
49
     *
50
     * @throws OAuth2Exception
51
     */
52
    public function handleBeforeAccessTokenIssuance(ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData
53
    {
54
        return call_user_func($this->getCallableBeforeAccessTokenIssuance(0), $request, $grantTypeData, $grantType);
55
    }
56
57
    /**
58
     * @param Client        $client
59
     * @param ResourceOwner $resourceOwner
60
     * @param AccessToken   $accessToken
61
     *
62
     * @return array
63
     *
64
     * @throws OAuth2Exception
65
     */
66
    public function handleAfterAccessTokenIssuance(Client $client, ResourceOwner $resourceOwner, AccessToken $accessToken): array
67
    {
68
        return call_user_func($this->getCallableAfterAccessTokenIssuance(0), $client, $resourceOwner, $accessToken);
69
    }
70
71
    /**
72
     * @param int $index
73
     *
74
     * @return callable
75
     *
76
     * @throws OAuth2Exception
77
     */
78
    private function getCallableBeforeAccessTokenIssuance(int $index): callable
79
    {
80
        if (!isset($this->extensions[$index])) {
81
            return function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData {
0 ignored issues
show
Unused Code introduced by
The parameter $grantType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
                return $grantTypeData;
83
            };
84
        }
85
        $extension = $this->extensions[$index];
86
87
        return function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType) use ($extension, $index): GrantTypeData {
88
            return $extension->beforeAccessTokenIssuance($request, $grantTypeData, $grantType, $this->getCallableBeforeAccessTokenIssuance($index + 1));
89
        };
90
    }
91
92
    /**
93
     * @param int $index
94
     *
95
     * @return callable
96
     *
97
     * @throws OAuth2Exception
98
     */
99
    private function getCallableAfterAccessTokenIssuance(int $index): callable
100
    {
101
        if (!array_key_exists($index, $this->extensions)) {
102
            return function (Client $client, ResourceOwner $resourceOwner, AccessToken $accessToken): array {
103
                return $accessToken->getResponseData();
104
            };
105
        }
106
        $extension = $this->extensions[$index];
107
108
        return function (Client $client, ResourceOwner $resourceOwner, AccessToken $accessToken) use ($extension, $index): array {
109
            return $extension->afterAccessTokenIssuance($client, $resourceOwner, $accessToken, $this->getCallableAfterAccessTokenIssuance($index + 1));
110
        };
111
    }
112
}
113