Server::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Phisch\OAuth\Server;
4
5
use Phisch\OAuth\Server\Exception\AuthorizationServerException;
6
use Phisch\OAuth\Server\Grant\GrantInterface;
7
use Phisch\OAuth\Server\Response\ResponseBuilderInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class Server
12
{
13
    /**
14
     * @var GrantInterface[]
15
     */
16
    private $grants = [];
17
18
    /**
19
     * @var ResponseBuilderInterface
20
     */
21
    private $responseBuilder;
22
23
    /**
24
     * AuthorizationServer constructor.
25
     * @param GrantInterface[] $grants
26
     * @param ResponseBuilderInterface $responseBuilder
27
     */
28
    public function __construct(array $grants, ResponseBuilderInterface $responseBuilder)
29
    {
30
        $this->grants = $grants;
31
        $this->responseBuilder = $responseBuilder;
32
    }
33
34
    /**
35
     * @param Request $request
36
     * @return mixed
37
     */
38
    public function tokenEndpoint(Request $request)
39
    {
40
        try {
41
            foreach ($this->grants as $grant) {
42
                if ($grant->supports($request)) {
43
                    return $grant->handle($request, $this->responseBuilder);
44
                }
45
            }
46
            throw new AuthorizationServerException(
47
                'The requested grant_type is unsupported.',
48
                null,
49
                null,
50
                'unsupported_grant_type'
51
            );
52
        } catch (AuthorizationServerException $exception) {
53
            return $this->responseBuilder->error($exception);
54
        }
55
    }
56
57
58
    /**
59
     * @param Request $request
60
     * @return Response|null
61
     */
62
    public function validateAuthorization(Request $request)
63
    {
64
        try {
65
            $this->getGrant($request)->validate($request);
66
        } catch (AuthorizationServerException $exception) {
67
            return $this->responseBuilder->error($exception);
68
        }
69
    }
70
71
    public function handleAuthorization(Request $request)
72
    {
73
        try {
74
            return $this->getGrant($request)->handle($request, $this->responseBuilder);
75
        } catch (AuthorizationServerException $exception) {
76
            return $this->responseBuilder->error($exception);
77
        }
78
    }
79
80
    public function cancelAuthorization(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
81
    {
82
        //return $this->getGrant($request)->cancel($request, $this->responseBuilder);
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
    }
84
85
    /**
86
     * @param $request
87
     * @return mixed|GrantInterface
88
     */
89
    private function getGrant($request)
90
    {
91
        foreach ($this->grants as $grant) {
92
            if ($grant->supports($request)) {
93
                return $grant;
94
            }
95
        }
96
        // TODO: throw invalid grant exception
97
    }
98
}
99