Completed
Push — master ( 326b3e...061c53 )
by Freek
09:13
created

BackupHasFailed   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toMail() 16 16 1
A toSlack() 19 19 1
A setEvent() 6 6 1

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\Backup\Notifications\Notifications;
4
5
use Illuminate\Notifications\Messages\MailMessage;
6
use Illuminate\Notifications\Messages\SlackAttachment;
7
use Illuminate\Notifications\Messages\SlackMessage;
8
use Spatie\Backup\Events\BackupHasFailed as BackupHasFailedEvent;
9
use Spatie\Backup\Notifications\BaseNotification;
10
11 View Code Duplication
class BackupHasFailed 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...
12
{
13
    /** @var \Spatie\Backup\Events\BackupHasFailed */
14
    protected $event;
15
16
    public function toMail(): MailMessage
17
    {
18
        $mailMessage = (new MailMessage)
19
            ->error()
20
            ->subject("Failed back up of `{$this->applicationName()}`")
21
            ->line("Important: An error occurred while backing up `{$this->applicationName()}`")
22
            ->line("Exception message: `{$this->event->exception->getMessage()}`")
23
            ->line("Exception trace: `{$this->event->exception->getTraceAsString()}`");
24
25
26
        $this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) {
27
            $mailMessage->line("{$name}: $value");
28
        });
29
30
        return $mailMessage;
31
    }
32
33
    public function toSlack(): SlackMessage
34
    {
35
        return (new SlackMessage)
36
            ->error()
37
            ->content("Failed back up of `{$this->applicationName()}`")
38
            ->attachment(function (SlackAttachment $attachment) {
39
                $attachment
40
                    ->title('Exception message')
41
                    ->content($this->event->exception->getMessage());
42
            })
43
            ->attachment(function (SlackAttachment $attachment) {
44
                $attachment
45
                    ->title('Exception trace')
46
                    ->content($this->event->exception->getTraceAsString());
47
            })
48
            ->attachment(function (SlackAttachment $attachment) {
49
                $attachment->fields($this->backupDestinationProperties()->toArray());
50
            });
51
    }
52
53
    public function setEvent(BackupHasFailedEvent $event)
54
    {
55
        $this->event = $event;
56
57
        return $this;
58
    }
59
}
60