Passed
Pull Request — master (#1074)
by Andrew
02:07
created

DeviceGrantMiddleware::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 14
ccs 0
cts 9
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace League\OAuth2\Server\Middleware;
4
5
use League\OAuth2\Server\Exception\OAuthServerException;
6
use League\OAuth2\Server\Repositories\DeviceAuthorizationRequestRepository;
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\MiddlewareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Http\Server\RequestHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\RequestHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class DeviceGrantMiddleware implements MiddlewareInterface
14
{
15
    private $deviceAuthorizationRequestRepository;
16
    private $responseFactory;
17
18
    public function __construct(
19
        DeviceAuthorizationRequestRepository $deviceAuthorizationRequestRepository,
20
        ResponseFactoryInterface $responseFactory
21
    )
22
    {
23
        $this->deviceAuthorizationRequestRepository = $deviceAuthorizationRequestRepository;
24
        $this->responseFactory = $responseFactory;
25
    }
26
27
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
28
    {
29
        $queryParameters = $request->getQueryParams();
30
        $deviceCode = $queryParameters['device_code'];
31
32
        // Get the last timestamp this client requested an access code
33
        $lastRequestTimeStamp = $this->deviceAuthorizationRequestRepository->getLast($deviceCode);
34
35
         // If the request is within the last 5 seconds, issue a slowdown notification
36
        if ($lastRequestTimeStamp + 5 > time()) {
37
            return OAuthServerException::slowDown()->generateHttpResponse($this->responseFactory->createResponse());
38
        }
39
40
        return $handler->handle($request);
41
    }
42
}