Completed
Push — master ( a85ca4...6010a8 )
by Derek Stephen
04:06
created

OAuthController::init()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 22
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Controller;
4
5
use Bone\Mvc\Controller;
6
use DateInterval;
7
use DateTime;
8
use League\OAuth2\Server\AuthorizationServer;
9
use League\OAuth2\Server\Exception\OAuthServerException;
10
use League\OAuth2\Server\Grant\PasswordGrant;
11
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
12
use OAuth\Repository\ClientRepository;
13
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
14
use OAuth2ServerExamples\Repositories\ScopeRepository;
15
use OAuth2ServerExamples\Repositories\UserRepository;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
19
class OAuthController extends Controller
20
{
21
    /** @var AuthorizationServer $oauth2Server */
22
    private $oauth2Server;
23
24
    public function init()
25
    {
26
        $this->oauth2Server = function () {
0 ignored issues
show
Documentation Bug introduced by
It seems like function () { $serve...; return $server; } of type object<Closure> is incompatible with the declared type object<League\OAuth2\Server\AuthorizationServer> of property $oauth2Server.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
            // Setup the authorization server
28
            $server = new AuthorizationServer(
29
                new ClientRepository(),                 // instance of ClientRepositoryInterface
0 ignored issues
show
Bug introduced by
The call to ClientRepository::__construct() misses some required arguments starting with $em.
Loading history...
30
                new AccessTokenRepository(),            // instance of AccessTokenRepositoryInterface
31
                new ScopeRepository(),                  // instance of ScopeRepositoryInterface
32
                'file://'.__DIR__.'/../private.key',    // path to private key
33
                'file://'.__DIR__.'/../public.key'      // path to public key
34
            );
35
36
            $grant = new PasswordGrant(
37
                new UserRepository(),           // instance of UserRepositoryInterface
38
                new RefreshTokenRepository()    // instance of RefreshTokenRepositoryInterface
39
            );
40
41
            $grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month
42
43
            // Enable the password grant on the server with a token TTL of 1 hour
44
            $server->enableGrantType(
45
                $grant,
46
                new DateInterval('PT1H') // access tokens will expire after 1 month
47
            );
48
            return $server;
49
        };
50
    }
51
52
    public function pingAction()
53
    {
54
        $date = new DateTime();
55
        $this->sendJsonResponse(['pong' => $date->format('Y-m-d H:i:s')]);
56
    }
57
}
58