MessageService::notifyObservers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace App\Utils;
4
5
use App\Http\Requests\ChangeStageRequest;
6
use App\Mail\CandidateMailable;
7
use App\Mail\NewCandidateNotification;
8
use App\Models\Candidate;
9
use App\Models\Message;
10
use App\Models\PredefinedMessage;
11
use Carbon\Carbon;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Mail;
14
15
class MessageService
16
{
17
    public static function sendConfirmationToCandidate($candidate)
18
    {
19
        $predefinedMessage = PredefinedMessage::where('recruitment_id', $candidate->recruitment->id)
20
            ->where('trigger', 'onApply')->first();
21
22
        if (!$predefinedMessage) {
23
            return false;
24
        }
25
26
        $message = new Message();
27
        $message->candidate_id = $candidate->id;
0 ignored issues
show
Bug introduced by
The property candidate_id does not exist on App\Models\Message. Did you mean candidate?
Loading history...
28
        $message->to = $candidate->email;
29
        //$message->from = '';
30
        $message->subject = ContentParser::parse($predefinedMessage->subject, $candidate).' '.UtilsService::hashSuffix($candidate->id);
31
        $message->body = ContentParser::parse($predefinedMessage->body, $candidate);
32
        $message->save();
33
34
        return self::send($message);
35
    }
36
37
    public static function sendMessage(Candidate $candidate, ChangeStageRequest $request)
38
    {
39
        //request->user()
40
        $message = new Message();
41
        $message->candidate_id = $candidate->id;
0 ignored issues
show
Bug introduced by
The property candidate_id does not exist on App\Models\Message. Did you mean candidate?
Loading history...
42
        $message->to = $candidate->email;
43
//        $message->from = '';
44
        $message->subject = $request->get('message_subject');
0 ignored issues
show
Bug introduced by
The method get() does not exist on App\Http\Requests\ChangeStageRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        /** @scrutinizer ignore-call */ 
45
        $message->subject = $request->get('message_subject');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        $message->body = $request->get('message_body');
46
47
        if ($request->get('delay_message_send')) {
48
            $delayUntil = Carbon::parse($request->get('delayed_message_date'));
49
            $message->scheduled_for = $delayUntil;
50
        }
51
52
        $message->save();
53
54
        return self::send($message);
55
    }
56
57
    public static function notifyObservers(Candidate $candidate)
58
    {
59
        $notificationEmail = $candidate->recruitment->notification_email;
60
61
        if (empty($notificationEmail)) {
62
            return false;
63
        }
64
65
        Mail::to($notificationEmail)->queue(new NewCandidateNotification($candidate));
66
67
        return true;
68
    }
69
70
    public static function notifyDeletedCandidate(Candidate $candidate, $emailAddress)
71
    {
72
        $predefinedMessage = PredefinedMessage::where('trigger', 'onDelete')->first();
73
74
        if (!$predefinedMessage) {
75
            return false;
76
        }
77
78
        $message = new Message();
79
        $message->candidate_id = $candidate->id;
0 ignored issues
show
Bug introduced by
The property candidate_id does not exist on App\Models\Message. Did you mean candidate?
Loading history...
80
        $message->to = $emailAddress;
81
        //$message->from = '';
82
        $message->subject = ContentParser::parse($predefinedMessage->subject, $candidate, null, Auth::user()); // . ' ' . UtilsService::hashSuffix($candidate->id);
83
        $message->body = ContentParser::parse($predefinedMessage->body, $candidate, null, Auth::user());
84
        $message->save();
85
86
        return self::send($message);
87
    }
88
89
    protected static function send(Message $message)
90
    {
91
        if ($message->scheduled_for) {
92
            Mail::to($message->to)->later($message->scheduled_for, new CandidateMailable($message));
0 ignored issues
show
Bug introduced by
$message->scheduled_for of type string is incompatible with the type DateInterval|DateTimeInterface|integer expected by parameter $delay of Illuminate\Mail\PendingMail::later(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            Mail::to($message->to)->later(/** @scrutinizer ignore-type */ $message->scheduled_for, new CandidateMailable($message));
Loading history...
93
        } else {
94
            Mail::to($message->to)->queue(new CandidateMailable($message));
95
        }
96
97
        return true;
98
    }
99
}
100