Completed
Push — master ( 0e9818...d1eb6b )
by Pascal
01:12
created

TraceUser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 16 4
A traceUserRequest() 0 17 3
A tooManyAttempts() 0 14 3
A shouldTraceUser() 0 8 2
1
<?php
2
3
namespace Protonemedia\LaravelTracer\Middleware;
4
5
use Closure;
6
use Illuminate\Cache\RateLimiter;
7
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Arr;
10
use Protonemedia\LaravelTracer\QualifiedRoute;
11
use Protonemedia\LaravelTracer\UserRequest;
12
use Symfony\Component\HttpFoundation\Response;
13
14
class TraceUser
15
{
16
    /**
17
     * Rate Limiter
18
     *
19
     * @var \Illuminate\Cache\RateLimiter
20
     */
21
    private $limiter;
22
23
    /**
24
     * Sets the Rate Limiter.
25
     *
26
     * @param \Illuminate\Cache\RateLimiter $limiter
27
     */
28
    public function __construct(RateLimiter $limiter)
29
    {
30
        $this->limiter = $limiter;
31
    }
32
33
    /**
34
     * Handle an incoming request and trace the user
35
     * if the current user is authenticated.
36
     *
37
     * @param  \Illuminate\Http\Request  $request
38
     * @param  \Closure  $next
39
     * @return mixed
40
     */
41
    public function handle($request, Closure $next)
42
    {
43
        $response = $next($request);
44
45
        if (!$user = $request->user()) {
46
            return $response;
47
        }
48
49
        if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
50
            return $response;
51
        }
52
53
        $this->traceUserRequest($user, $request, $response);
54
55
        return $response;
56
    }
57
58
    /**
59
     * Stores the user request in the database.
60
     *
61
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
62
     * @param  \Illuminate\Http\Request  $request
63
     * @param  \Symfony\Component\HttpFoundation\Response  $response
64
     *
65
     * @return \Protonemedia\LaravelTracer\UserRequest|null
66
     */
67
    private function traceUserRequest(UserContract $user, Request $request, Response $response):  ? UserRequest
68
    {
69
        $qualified = $request->qualifiedRoute();
70
71
        if ($this->tooManyAttempts($qualified)) {
72
            return null;
73
        }
74
75
        if (!$this->shouldTraceUser($request, $response)) {
76
            return null;
77
        }
78
79
        return UserRequest::create([
80
            'user_id'         => $user->getAuthIdentifier(),
81
            'qualified_route' => $qualified->name(),
82
        ]);
83
    }
84
85
    /**
86
     * Returns a boolean wether this request has been attemped to
87
     * trace too many times.
88
     *
89
     * @param  array  $qualified
90
     * @return boolean
91
     */
92
    private function tooManyAttempts($qualified) : bool
93
    {
94
        if (!$secondsBetweenLogs = $qualified->secondsBetweenLogs()) {
0 ignored issues
show
Bug introduced by
The method secondsBetweenLogs cannot be called on $qualified (of type array).

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...
95
            return false;
96
        }
97
98
        if ($this->limiter->tooManyAttempts($qualified->name(), 1)) {
0 ignored issues
show
Bug introduced by
The method name cannot be called on $qualified (of type array).

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...
99
            return true;
100
        }
101
102
        $this->limiter->hit($qualified->name(), $secondsBetweenLogs);
0 ignored issues
show
Bug introduced by
The method name cannot be called on $qualified (of type array).

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...
103
104
        return false;
105
    }
106
107
    /**
108
     * Calls the callable method that can be specified in the config file.
109
     *
110
     * @param  \Illuminate\Http\Request  $request
111
     * @param  \Symfony\Component\HttpFoundation\Response $response
112
     *
113
     * @return boolean
114
     */
115
    private function shouldTraceUser(Request $request, Response $response): bool
116
    {
117
        if (!$callable = config('laravel-tracer.should_trace_user')) {
118
            return true;
119
        }
120
121
        return app()->call($callable, [$request, $response]);
122
    }
123
}
124