RefreshSessionMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 7
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __invoke() 0 23 3
1
<?php
2
3
namespace Bunq\Middleware;
4
5
use Bunq\Exception\SessionWasExpiredException;
6
use Bunq\Service\InstallationService;
7
use Bunq\Token\DefaultToken;
8
use Bunq\Token\Storage\TokenStorage;
9
use Bunq\Token\TokenType;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * bunq documentation:
14
 * A session expires after the same amount of time you have set for auto logout in your user account. If a request is
15
 * made 30 seconds before a session expires, it will automatically be extended.
16
 *
17
 * This middleware detects if the session has expired, if so, the middleware will renew the session and throws an
18
 * exception to let the client know it can be retried.
19
 */
20
final class RefreshSessionMiddleware
21
{
22
    /**
23
     * @var InstallationService
24
     */
25
    private $installationService;
26
27
    /**
28
     * @var TokenStorage
29
     */
30
    private $tokenStorage;
31
32
    /**
33
     * @param InstallationService $installationService
34
     * @param TokenStorage        $tokenStorage
35
     */
36
    public function __construct(
37
        InstallationService $installationService,
38
        TokenStorage $tokenStorage
39
    ) {
40
        $this->installationService = $installationService;
41
        $this->tokenStorage        = $tokenStorage;
42
    }
43
44
    /**
45
     * @param ResponseInterface $response
46
     *
47
     * @return ResponseInterface
48
     * @throws \Exception
49
     */
50
    public function __invoke(ResponseInterface $response)
51
    {
52
        if ($response->getStatusCode() !== 401) {
53
            return $response;
54
        }
55
56
        $responseBody = \json_decode($response->getBody()->__toString(), true);
57
58
        if ($responseBody['Error'][0]['error_description'] !== 'Insufficient authorisation.') {
59
            return $response;
60
        }
61
62
        // make new session
63
64
        $installationToken = $currentInstalltionToken = $this->tokenStorage->load(TokenType::INSTALLATION_TOKEN());
0 ignored issues
show
Unused Code introduced by
$currentInstalltionToken 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...
65
        $session           = $this->installationService->createSession($installationToken);
66
67
        $sessionToken = DefaultToken::fromString($session);
0 ignored issues
show
Documentation introduced by
$session is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
69
        $this->tokenStorage->save($sessionToken, TokenType::SESSION_TOKEN());
70
71
        throw new SessionWasExpiredException();
72
    }
73
74
}
75