RouteRequested   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 130
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 26
loc 130
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A via() 14 14 4
B toMail() 6 27 6
B toSlack() 6 35 6
A toArray() 0 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 Audit\Notifications\Logs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Notifications\Notification;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Notifications\Messages\MailMessage;
9
use Illuminate\Notifications\Messages\SlackMessage;
10
use Audit\Models\Logs\LaraLogsRequest;
11
12
class RouteRequested extends Notification implements ShouldQueue
13
{
14
    use Queueable;
15
16
    private $laraLogsRequest;
17
18
    private $requestInfo;
19
20
    /**
21
     * Create a new notification instance.
22
     *
23
     * @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...
24
     */
25
    public function __construct(LaraLogsRequest $laraLogsRequest)
26
    {
27
        $this->laraLogsRequest = $laraLogsRequest;
28
29
        $this->requestInfo = array(
30
            'id' => $laraLogsRequest->id,
31
            'method' => $laraLogsRequest->method,
32
            'uri' => $laraLogsRequest->uri,
33
            'ip' => $laraLogsRequest->ip,
34
            'execution_time' => floor(($laraLogsRequest->end_time - $laraLogsRequest->start_time) * 1000)
35
        );
36
    }
37
38
    /**
39
     * Get the notification's delivery channels.
40
     *
41
     * @param  mixed $notifiable
42
     * @return array
43
     */
44 View Code Duplication
    public function via($notifiable)
0 ignored issues
show
Duplication introduced by
This method 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...
45
    {
46
        switch($notifiable->notify_by) {
47
        case 'email':
48
            return ['mail'];
49
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
50
        case 'slack':
51
            return ['slack'];
52
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
53
        case 'email_slack':
54
            return ['mail', 'slack'];
55
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
56
        }
57
    }
58
59
    /**
60
     * Get the mail representation of the notification.
61
     *
62
     * @param  mixed $notifiable
63
     * @return \Illuminate\Notifications\Messages\MailMessage
64
     */
65
    public function toMail($notifiable)
66
    {
67
        $content = 'A route on ' . url('/') . ' was requested.';
68 View Code Duplication
        if($notifiable->filter !== '*' && !is_numeric($notifiable->filter)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
            $content .= " You're being notified because the route contains `" . $notifiable->filter . "`";
70
        }
71
72
        if(is_numeric($notifiable->filter)) {
73
            $content .= " You're being notified because the execution time exceeded your limit of " . $notifiable->filter . "ms.";
74
        }
75
76
        $alertColor = '#00B945';
77 View Code Duplication
        if(is_numeric($notifiable->filter) && $this->requestInfo['execution_time'] > intval($notifiable->filter)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
78
            $alertColor = '#BC001A';
79
        }
80
81
        return (new MailMessage)
82
            ->subject(env('LARALogs_ROUTE_SUBJECT', '[LaraLogs Alert] A route has been requested'))
83
            ->from(env('LARALogs_FROM_EMAIL', '[email protected]'), env('LARALogs_FROM_NAME', 'LaraLogs Alerts'))
84
            ->view(
85
                'laraLogs::emails.route-requested', [
86
                'requestInfo' => $this->requestInfo,
87
                'content' => $content,
88
                'alertColor' => $alertColor
89
                ]
90
            );
91
    }
92
93
    public function toSlack($notifiable)
94
    {
95
        $content = 'A route on ' . url('/') . ' was requested.';
96 View Code Duplication
        if($notifiable->filter !== '*' && !is_numeric($notifiable->filter)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
97
            $content .= " You're being notified because the route contains `" . $notifiable->filter . "`";
98
        }
99
100
        if(is_numeric($notifiable->filter)) {
101
            $content .= " You're being notified because the execution time exceeded your limit of " . $notifiable->filter . "ms.";
102
        }
103
104
        $status = 'success';
105 View Code Duplication
        if(is_numeric($notifiable->filter) && $this->requestInfo['execution_time'] > intval($notifiable->filter)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
106
            $status = 'error';
107
        }
108
109
        $requestInfo = $this->requestInfo;
110
111
        return (new SlackMessage)
112
            ->{ $status }()
113
            ->attachment(
114
                function ($attachment) use ($requestInfo, $content) {
115
                    $attachment->title('Request #' . $requestInfo['id'], route('laraLogs::requests.show', $requestInfo['id']))
116
                        ->content($content)
117
                        ->fields(
118
                            [
119
                            'Method' => $requestInfo['method'],
120
                            'From IP' => $requestInfo['ip'],
121
                            'Requested URI' => $requestInfo['uri'],
122
                            'Execution Time' => $requestInfo['execution_time'] . 'ms'
123
                            ]
124
                        );
125
                }
126
            );
127
    }
128
129
    /**
130
     * Get the array representation of the notification.
131
     *
132
     * @param  mixed $notifiable
133
     * @return array
134
     */
135
    public function toArray($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...
136
    {
137
        return [
138
            //
139
        ];
140
    }
141
}
142