Completed
Push — master ( 0f5e6b...d874fd )
by Conrad
01:54
created

AuthoriseController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 9
dl 0
loc 67
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B index() 0 30 5
A getErrorResponse() 0 7 1
A convertResponse() 0 4 1
1
<?php
2
3
namespace AdvancedLearning\Oauth2Server\Controllers;
4
5
use AdvancedLearning\Oauth2Server\AuthorizationServer\Generator;
6
use Exception;
7
use GuzzleHttp\Psr7\Response;
8
use League\OAuth2\Server\Exception\OAuthServerException;
9
use Psr\Http\Message\ResponseInterface;
10
use Robbie\Psr7\HttpRequestAdapter;
11
use Robbie\Psr7\HttpResponseAdapter;
12
use SilverStripe\Control\Controller;
13
use SilverStripe\Control\HTTP;
14
use SilverStripe\Control\HTTPResponse;
15
16
class AuthoriseController extends Controller
17
{
18
    /**
19
     * @var Generator
20
     */
21
    protected $serverGenerator;
22
23
    /**
24
     * AuthoriseController constructor. If no Authorization Service is passed a default one is created.
25
     *
26
     * @param Generator $serverGenerator
27
     */
28
    public function __construct(Generator $serverGenerator)
29
    {
30
        $this->serverGenerator = $serverGenerator;
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Handles authorisation.
36
     *
37
     * @return HTTPResponse
38
     */
39
    public function index(): HTTPResponse
40
    {
41
        $body = null;
0 ignored issues
show
Unused Code introduced by Conrad Dobbs
$body is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
42
43
        if ($this->getRequest()->getHeader('Content-Type') === 'application/json') {
44
            $body = json_decode($this->getRequest()->getBody(), true);
45
        } else {
46
            $body = $this->getRequest()->postVars();
47
        }
48
49
        if (empty($body)) {
50
            return $this->jsonResponse(['error' => 'No parameters could be found in request body'], 500);
0 ignored issues
show
Documentation Bug introduced by Conrad Dobbs
The method jsonResponse does not exist on object<AdvancedLearning\...rs\AuthoriseController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
51
        }
52
53
        // request needs parsed body
54
        $psrRequest = (new HttpRequestAdapter())->toPsr7($this->getRequest())
55
            ->withParsedBody($body);
56
        $psrResponse = new Response();
57
58
        $authServer = $this->serverGenerator->getServer();
59
60
        try {
61
            return (new HttpResponseAdapter())
62
                ->fromPsr7($authServer->respondToAccessTokenRequest($psrRequest, $psrResponse));
63
        } catch (OAuthServerException $e) {
64
            return $this->convertResponse($e->generateHttpResponse(new Response()));
65
        } catch (Exception $e) {
66
            return $this->getErrorResponse($e->getMessage());
67
        }
68
    }
69
70
    protected function getErrorResponse($message, $responseCode = 500)
71
    {
72
        $response = (new OAuthServerException($message, 100, 'server_error', $responseCode))
73
            ->generateHttpResponse(new Response());
74
75
        return $this->convertResponse($response);
76
    }
77
78
    protected function convertResponse(ResponseInterface $response)
79
    {
80
        return (new HttpResponseAdapter())->fromPsr7($response);
81
    }
82
}
83