GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RoutesThrottledNotifications   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 53
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A limiter() 0 4 1
A throttleKey() 0 6 1
A maxAttempts() 0 4 1
1
<?php
2
3
namespace TalvBansal\ThrottledFailedJobMonitor;
4
5
use Illuminate\Cache\RateLimiter;
6
use Illuminate\Notifications\RoutesNotifications;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Str;
9
use TalvBansal\ThrottledFailedJobMonitor\Event\NotificationLimitReached;
10
11
/**
12
 * Trait RoutesThrottledNotifications.
13
 */
14
trait RoutesThrottledNotifications
15
{
16
    use RoutesNotifications {
17
        RoutesNotifications::notify as parentNotify;
18
    }
19
20 5
    public function notify($instance): void
21
    {
22 5
        if ($instance instanceof ThrottledNotification) {
23 5
            $key = $this->throttleKey($instance);
24 5
            if ($this->limiter()->tooManyAttempts($key, $this->maxAttempts())) {
25 1
                Log::notice("Skipping sending notification with key `$key`. Rate limit reached.");
26 1
                event(new NotificationLimitReached($key));
27
28 1
                return;
29
            }
30
31 5
            $this->limiter()->hit($key, ($instance->throttleDecayMinutes() * 60));
32
        }
33
34
        // Execute the original notify() method.
35 5
        $this->parentNotify($instance);
0 ignored issues
show
Bug introduced by
The method parentNotify() does not exist on TalvBansal\ThrottledFail...sThrottledNotifications. Did you maybe mean notify()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
36 5
    }
37
38
    /**
39
     * Get the rate limiter instance.
40
     */
41 5
    protected function limiter(): RateLimiter
42
    {
43 5
        return app(RateLimiter::class);
44
    }
45
46
    /**
47
     * Build the notification throttle key from the Notification class name,
48
     * the Notification's throttle key id.
49
     * @param ThrottledNotification $instance
50
     * @return string
51
     */
52 5
    protected function throttleKey(ThrottledNotification $instance)
53
    {
54 5
        return Str::kebab(
55 5
            class_basename($instance).'-'.$instance->throttleKeyId()
56
        );
57
    }
58
59
    /**
60
     * Set the max attempts to 1.
61
     */
62 5
    protected function maxAttempts()
63
    {
64 5
        return 1;
65
    }
66
}
67