1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Passport\Package; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Passport\Contracts\PassportServerInterface; |
22
|
|
|
use Psr\Container\ContainerInterface; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
25
|
|
|
use function assert; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @package Limoncello\Passport |
29
|
|
|
*/ |
30
|
|
|
class PassportController |
31
|
|
|
{ |
32
|
|
|
/** @var callable */ |
33
|
|
|
const AUTHORIZE_HANDLER = [self::class, 'authorize']; |
34
|
|
|
|
35
|
|
|
/** @var callable */ |
36
|
|
|
const TOKEN_HANDLER = [self::class, 'token']; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $routeParams |
40
|
|
|
* @param ContainerInterface $container |
41
|
|
|
* @param ServerRequestInterface $request |
42
|
|
|
* |
43
|
|
|
* @return ResponseInterface |
44
|
|
|
*/ |
45
|
1 |
View Code Duplication |
public static function authorize( |
|
|
|
|
46
|
|
|
array $routeParams, |
47
|
|
|
ContainerInterface $container, |
48
|
|
|
ServerRequestInterface $request |
49
|
|
|
): ResponseInterface { |
50
|
1 |
|
assert($routeParams !== null && $request !== null); |
51
|
|
|
|
52
|
|
|
/** @var PassportServerInterface $passportServer */ |
53
|
1 |
|
$passportServer = $container->get(PassportServerInterface::class); |
54
|
1 |
|
$tokenResponse = $passportServer->getCreateAuthorization($request); |
55
|
|
|
|
56
|
1 |
|
return $tokenResponse; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $routeParams |
61
|
|
|
* @param ContainerInterface $container |
62
|
|
|
* @param ServerRequestInterface $request |
63
|
|
|
* |
64
|
|
|
* @return ResponseInterface |
65
|
|
|
*/ |
66
|
1 |
View Code Duplication |
public static function token( |
|
|
|
|
67
|
|
|
array $routeParams, |
68
|
|
|
ContainerInterface $container, |
69
|
|
|
ServerRequestInterface $request |
70
|
|
|
): ResponseInterface { |
71
|
1 |
|
assert($routeParams !== null && $request !== null); |
72
|
|
|
|
73
|
|
|
/** @var PassportServerInterface $passportServer */ |
74
|
1 |
|
$passportServer = $container->get(PassportServerInterface::class); |
75
|
1 |
|
$tokenResponse = $passportServer->postCreateToken($request); |
76
|
|
|
|
77
|
1 |
|
return $tokenResponse; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.