Completed
Push — master ( a206dc...d306f8 )
by Derek Stephen
02:37
created

OAuthController::init()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 30
rs 8.8571
c 1
b 0
f 0
ccs 0
cts 20
cp 0
cc 1
eloc 21
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Controller;
4
5
use Bone\Mvc\Controller;
6
use DateTime;
7
use Del\Common\ContainerService;
8
use OAuth2\GrantType\ClientCredentials;
9
use OAuth2\GrantType\AuthorizationCode;
10
use OAuth2\GrantType\RefreshToken;
11
use OAuth2\Server;
12
use Psr\Http\Message\ResponseInterface;
13
use Zend\Diactoros\Response\SapiEmitter;
14
15
class OAuthController extends Controller
16
{
17
    /** @var Server $oauth2Server */
18
    protected $oauth2Server;
19
20
    public function init()
21
    {
22
        $container = ContainerService::getInstance()->getContainer();
23
        $clientRepository = $container['repository.Client'];
24
        $accessTokenRepository = $container['repository.AccessToken'];
25
        $authCodeRepository = $container['repository.AuthCode'];
26
        $scopeRepository = $container['repository.Scope'];
0 ignored issues
show
Unused Code introduced by
$scopeRepository 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...
27
        $userRepository = $container['repository.User'];
28
        $refreshTokenRepository = $container['repository.RefreshToken'];
29
30
31
        $server = new Server([
32
            'client_credentials' => $clientRepository,
33
            'user_credentials'   => $userRepository,
34
            'access_token'       => $accessTokenRepository,
35
            'authorization_code' => $authCodeRepository,
36
            'refresh_token'      => $refreshTokenRepository,
37
        ], [
38
            'auth_code_lifetime' => 30,
39
            'refresh_token_lifetime' => 30,
40
        ]);
41
42
        $server->addGrantType(new ClientCredentials($clientRepository));
43
        $server->addGrantType(new AuthorizationCode($authCodeRepository));
44
        $server->addGrantType(new RefreshToken($refreshTokenRepository, [
45
            'always_issue_new_refresh_token' => true,
46
        ]));
47
48
        $this->oauth2Server = $server;
49
    }
50
51
52
    /**
53
     * Sends a response with the time
54
     */
55
    public function pingAction()
56
    {
57
        $date = new DateTime();
58
        $this->sendJsonResponse(['pong' => $date->format('Y-m-d H:i:s')]);
59
    }
60
61
    /**
62
     * @param ResponseInterface $response
63
     */
64
    public function sendResponse(ResponseInterface $response)
65
    {
66
        $emitter = new SapiEmitter();
67
        $emitter->emit($response);
68
        exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method sendResponse() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
69
    }
70
}
71