Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SendApprovalEmail
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildMail
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
5
 logLabel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 gateBucket
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 gateCompanyId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Jobs\Email;
4
5use App\Services\SendgridDispatcher;
6use SendGrid\Mail\Mail;
7
8/**
9 * FIRE-1147: async wrapper for the "needs approval" notification sent when
10 * a quote crosses an approval threshold. Replaces the inline SendGrid call
11 * in Quotations::send_approval_notification (Quotations.php:247-…).
12 *
13 * The controller still does the DB queries + template rendering + records
14 * the approval-pending state synchronously (FIRE-1092 ordering: record
15 * first, send second). This job just delivers the pre-rendered email so
16 * the user's HTTP response can return in < 200 ms.
17 */
18class SendApprovalEmail extends AbstractSendgridJob
19{
20    public function __construct(
21        public readonly string $toEmail,
22        public readonly string $subject,
23        public readonly string $html,
24        /** Optional BCC addresses (commercial / creator copies). */
25        public readonly array $bcc = [],
26        public readonly ?string $quoteId = null,
27        /** FIRE-1930: quotation's company, checked against the approval kill switch. */
28        public readonly ?int $companyId = null,
29    ) {}
30
31    protected function buildMail(): ?Mail
32    {
33        if ($this->toEmail === '') {
34            return null;
35        }
36
37        $email = new Mail;
38        $email->setFrom('fire@fire.es', 'Fire Service Titan');
39        $email->setSubject($this->subject);
40        $email->addTo($this->toEmail);
41        $email->addContent('text/html', $this->html);
42
43        foreach ($this->bcc as $address) {
44            if ($address && $address !== $this->toEmail) {
45                $email->addBcc($address);
46            }
47        }
48
49        $imgpath = file_get_contents(public_path('fireservicetitan.png'));
50        $email->addAttachment(
51            $imgpath,
52            'image/png',
53            'fireservicetitan.png',
54            'inline',
55            'fireservicetitan'
56        );
57
58        return $email;
59    }
60
61    protected function logLabel(): string
62    {
63        return 'Quotations::send_approval_notification';
64    }
65
66    protected function gateBucket(): ?string
67    {
68        return SendgridDispatcher::BUCKET_APPROVAL;
69    }
70
71    protected function gateCompanyId(): ?int
72    {
73        // ?? so a job queued before this property existed gates as null
74        // instead of reading an uninitialized readonly property.
75        return $this->companyId ?? null;
76    }
77}