Passed
Push — task/add-applicants-and-applic... ( 514472...36c932 )
by Yonathan
10:54 queued 12s
created

MicroReferenceController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 37
dl 0
loc 91
rs 10
c 2
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A showSecondaryReferenceEmail() 0 4 1
A sendSecondaryReferenceEmail() 0 21 3
A sendDirectorEmail() 0 21 3
A index() 0 9 1
A showDirectorEmail() 0 4 1
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Exceptions\SendEmailException;
6
use App\Http\Controllers\Controller;
7
use App\Mail\MicroReferenceMail;
8
use App\Models\ApplicationReview;
9
use App\Models\JobApplication;
10
use Illuminate\Support\Facades\Lang;
11
use Illuminate\Support\Facades\Mail;
12
13
class MicroReferenceController extends Controller
14
{
15
16
    /**
17
     * Get all reference emails created for an application.
18
     *
19
     * @param JobApplication $application The application the references are for.
20
     *
21
     * @return \Illuminate\Http\Response
22
     */
23
    public function index(JobApplication $application)
24
    {
25
        $directorMail = new MicroReferenceMail($application, true);
26
        $referenceMail = new MicroReferenceMail($application, false);
27
        $mails = [
28
            'director' => $directorMail->toArray(),
29
            'secondary' => $referenceMail->toArray(),
30
        ];
31
        return response()->json($mails);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

31
        return /** @scrutinizer ignore-call */ response()->json($mails);
Loading history...
32
    }
33
34
    /**
35
     * Get email created to be sent to an application's Director reference.
36
     *
37
     * @param JobApplication $application The application the reference is for.
38
     *
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function showDirectorEmail(JobApplication $application)
42
    {
43
        $mail = new MicroReferenceMail($application, true);
44
        return response()->json($mail->toArray());
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

44
        return /** @scrutinizer ignore-call */ response()->json($mail->toArray());
Loading history...
45
    }
46
47
    public function sendDirectorEmail(JobApplication $application)
48
    {
49
        $mail = new MicroReferenceMail($application, true);
50
        $mail->build();
51
        if ($mail->to === []) {
52
            throw new SendEmailException(Lang::get('errors.email_missing_delivery_address'));
53
        }
54
55
        Mail::send($mail);
56
57
        $review = $application->application_review;
58
        if ($review === null) {
59
            $review = new ApplicationReview();
60
            $review->job_application()->associate($application);
61
        }
62
        $review->director_email_sent = true;
63
        $review->save();
64
65
        // Recreate the mail, because being calling ->toArray() after ->build() can cause some fields to double.
66
        $mail = new MicroReferenceMail($application, true);
67
        return response()->json($mail->toArray());
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

67
        return /** @scrutinizer ignore-call */ response()->json($mail->toArray());
Loading history...
68
    }
69
70
    /**
71
     * Get email created to be sent to an application's Secondary reference.
72
     *
73
     * @param JobApplication $application The application the reference is for.
74
     *
75
     * @return \Illuminate\Http\Response
76
     */
77
    public function showSecondaryReferenceEmail(JobApplication $application)
78
    {
79
        $mail = new MicroReferenceMail($application, false);
80
        return response()->json($mail->toArray());
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

80
        return /** @scrutinizer ignore-call */ response()->json($mail->toArray());
Loading history...
81
    }
82
83
    public function sendSecondaryReferenceEmail(JobApplication $application)
84
    {
85
        $mail = new MicroReferenceMail($application, false);
86
        $mail->build();
87
        if ($mail->to === []) {
88
            throw new SendEmailException(Lang::get('errors.email_missing_delivery_address'));
89
        }
90
91
        Mail::send($mail);
92
93
        $review = $application->application_review;
94
        if ($review === null) {
95
            $review = new ApplicationReview();
96
            $review->job_application()->associate($application);
97
        }
98
        $review->reference_email_sent = true;
99
        $review->save();
100
101
        // Recreate the mail, because being calling ->toArray() after ->build() can cause some fields to double.
102
        $mail = new MicroReferenceMail($application, false);
103
        return response()->json($mail->toArray());
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

103
        return /** @scrutinizer ignore-call */ response()->json($mail->toArray());
Loading history...
104
    }
105
}
106