Completed
Push — master ( 592d95...c00565 )
by Abdelrahman
18:11 queued 10s
created

CreateFreshApiToken   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 113
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 15 2
A shouldReceiveFreshToken() 0 5 2
A requestShouldReceiveFreshToken() 0 4 2
A responseShouldReceiveFreshToken() 0 6 3
A alreadyContainsToken() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Http\Middleware;
6
7
use Closure;
8
use Illuminate\Http\Response;
9
use Illuminate\Http\JsonResponse;
10
use Rinvex\Oauth\Factories\ApiTokenCookieFactory;
11
12
class CreateFreshApiToken
13
{
14
    /**
15
     * The API token cookie factory instance.
16
     *
17
     * @var \Rinvex\Oauth\Factories\ApiTokenCookieFactory
18
     */
19
    protected $cookieFactory;
20
21
    /**
22
     * The authentication guard.
23
     *
24
     * @var string
25
     */
26
    protected $guard;
27
28
    /**
29
     * Create a new middleware instance.
30
     *
31
     * @param \Rinvex\Oauth\Factories\ApiTokenCookieFactory $cookieFactory
32
     *
33
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
34
     */
35
    public function __construct(ApiTokenCookieFactory $cookieFactory)
36
    {
37
        $this->cookieFactory = $cookieFactory;
38
    }
39
40
    /**
41
     * Handle an incoming request.
42
     *
43
     * @param \Illuminate\Http\Request $request
44
     * @param \Closure                 $next
45
     * @param string|null              $guard
46
     *
47
     * @return mixed
48
     */
49
    public function handle($request, Closure $next, $guard = null)
50
    {
51
        $this->guard = $guard;
52
53
        $response = $next($request);
54
55
        if ($this->shouldReceiveFreshToken($request, $response)) {
56
            $response->withCookie($this->cookieFactory->make(
57
                $request->user($this->guard)->getAuthIdentifier(),
58
                $request->session()->token()
0 ignored issues
show
Bug introduced by
The method token cannot be called on $request->session() (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
59
            ));
60
        }
61
62
        return $response;
63
    }
64
65
    /**
66
     * Determine if the given request should receive a fresh token.
67
     *
68
     * @param \Illuminate\Http\Request  $request
69
     * @param \Illuminate\Http\Response $response
70
     *
71
     * @return bool
72
     */
73
    protected function shouldReceiveFreshToken($request, $response)
74
    {
75
        return $this->requestShouldReceiveFreshToken($request) &&
76
               $this->responseShouldReceiveFreshToken($response);
77
    }
78
79
    /**
80
     * Determine if the request should receive a fresh token.
81
     *
82
     * @param \Illuminate\Http\Request $request
83
     *
84
     * @return bool
85
     */
86
    protected function requestShouldReceiveFreshToken($request)
87
    {
88
        return $request->isMethod('GET') && $request->user($this->guard);
89
    }
90
91
    /**
92
     * Determine if the response should receive a fresh token.
93
     *
94
     * @param \Illuminate\Http\Response $response
95
     *
96
     * @return bool
97
     */
98
    protected function responseShouldReceiveFreshToken($response)
99
    {
100
        return ($response instanceof Response ||
101
                $response instanceof JsonResponse) &&
102
                ! $this->alreadyContainsToken($response);
103
    }
104
105
    /**
106
     * Determine if the given response already contains an API token.
107
     *
108
     * This avoids us overwriting a just "refreshed" token.
109
     *
110
     * @param \Illuminate\Http\Response $response
111
     *
112
     * @return bool
113
     */
114
    protected function alreadyContainsToken($response)
115
    {
116
        foreach ($response->headers->getCookies() as $cookie) {
117
            if ($cookie->getName() === config('rinvex.oauth.cookie')) {
118
                return true;
119
            }
120
        }
121
122
        return false;
123
    }
124
}
125