CheckFailed   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 4
dl 48
loc 48
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toMail() 7 7 1
A toSlack() 12 12 1
A setEvent() 6 6 1
A shouldSend() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spatie\ServerMonitor\Notifications\Notifications;
4
5
use Carbon\Carbon;
6
use Illuminate\Notifications\Messages\MailMessage;
7
use Illuminate\Notifications\Messages\SlackAttachment;
8
use Illuminate\Notifications\Messages\SlackMessage;
9
use Spatie\ServerMonitor\Events\CheckFailed as CheckFailedEvent;
10
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
11
use Spatie\ServerMonitor\Notifications\BaseNotification;
12
13 View Code Duplication
class CheckFailed extends BaseNotification
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /** @var \Spatie\ServerMonitor\Events\CheckWarning */
16
    public $event;
17
18
    /**
19
     * Get the mail representation of the notification.
20
     *
21
     * @param  mixed $notifiable
22
     * @return \Illuminate\Notifications\Messages\MailMessage
23
     */
24
    public function toMail($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        return (new MailMessage)
27
            ->error()
28
            ->subject($this->getSubject())
29
            ->line($this->getMessageText());
30
    }
31
32
    public function toSlack($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        return (new SlackMessage)
35
            ->error()
36
            ->attachment(function (SlackAttachment $attachment) {
37
                $attachment
38
                    ->title($this->getSubject())
39
                    ->content($this->getMessageText())
40
                    ->fallback($this->getMessageText())
41
                    ->timestamp(Carbon::now());
42
            });
43
    }
44
45
    public function setEvent(CheckFailedEvent $event)
46
    {
47
        $this->event = $event;
0 ignored issues
show
Documentation Bug introduced by
It seems like $event of type object<Spatie\ServerMonitor\Events\CheckFailed> is incompatible with the declared type object<Spatie\ServerMonitor\Events\CheckWarning> of property $event.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
49
        return $this;
50
    }
51
52
    public function shouldSend(): bool
53
    {
54
        if (! $this->getCheck()->hasStatus(CheckStatus::FAILED)) {
55
            return false;
56
        }
57
58
        return ! $this->getCheck()->isThrottlingFailedNotifications();
59
    }
60
}
61