JobsCollected::toMail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php namespace JobApis\JobsToMail\Notifications;
2
3
use Illuminate\Bus\Queueable;
4
use Illuminate\Notifications\Notification;
5
use Illuminate\Contracts\Queue\ShouldQueue;
6
use Illuminate\Queue\SerializesModels;
7
use JobApis\JobsToMail\Models\Search;
8
use JobApis\JobsToMail\Notifications\Messages\JobMailMessage;
9
10
class JobsCollected extends Notification implements ShouldQueue
11
{
12
    use Queueable, SerializesModels;
13
14
    /**
15
     * @var array of Job objects
16
     */
17
    protected $jobs;
18
19
    /**
20
     * @var Search
21
     */
22
    protected $search;
23
24
    /**
25
     * Create a new notification instance.
26
     *
27
     * @param array $jobs
28
     * @param Search $search
29
     *
30
     * @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...
31
     */
32 4
    public function __construct(array $jobs, Search $search)
33
    {
34 4
        $this->jobs = $jobs;
35 4
        $this->search = $search;
36 4
    }
37
38
    /**
39
     * Get the notification's delivery channels.
40
     *
41
     * @param  mixed  $notifiable
42
     * @return array
43
     */
44 1
    public function via($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...
45
    {
46 1
        return ['mail', 'database'];
47
    }
48
49
    /**
50
     * Get the array representation of the notification for saving to the database.
51
     *
52
     * @param  mixed  $notifiable
53
     * @return array
54
     */
55 1
    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...
56
    {
57
        return [
58 1
            'search_id' => $this->search->id,
59 1
            'jobs' => $this->jobs,
60
        ];
61
    }
62
63
    /**
64
     * Get the mail representation of the notification.
65
     *
66
     * @param  mixed  $notifiable
67
     * @return \Illuminate\Notifications\Messages\MailMessage
68
     */
69 1
    public function toMail($notifiable)
70
    {
71
        // Instantiate the message
72 1
        $message = new JobMailMessage();
73
74
        // Add user and search ID to view data
75 1
        $message->viewData['user_id'] = $notifiable->id;
76 1
        $message->viewData['search_id'] = $this->search->id;
77
78
        // Update the subject
79 1
        $message->subject(count($this->jobs).' new jobs found especially for you');
80
81
        // Add a jobs-hub ad
82 1
        $message->advertisement('upgrade');
83
84
        // Update the message text
85 1
        $message->greeting('Hello,')
86 1
            ->line('We found the following jobs that we think 
87
                you\'ll be interested in based on your search:')
88 1
            ->line("\"{$this->search->keyword} in {$this->search->location}\"");
89
90
        // Add jobs
91 1
        foreach (array_slice($this->jobs, 0, 10) as $job) {
92 1
            $message->listing($job);
93
        }
94
95
        // Add a link to download the collection
96 1
        $message->action(
97 1
            'View More',
98 1
            url('/notifications/' . $this->id)
0 ignored issues
show
Bug introduced by
It seems like url('/notifications/' . $this->id) targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Illuminate\Notifications...SimpleMessage::action() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
99
        );
100
101 1
        return $message;
102
    }
103
}
104