Issues (6)

src/MailCenter/SendMail.php (2 issues)

1
<?php
2
3
namespace Sfneal\PostOffice\MailCenter;
4
5
use Illuminate\Mail\Mailable;
6
use Illuminate\Support\Facades\Mail;
7
use Sfneal\Queueables\Job;
8
9
class SendMail extends Job
10
{
11
    /**
12
     * @var string Email address of recipient
13
     */
14
    public $to;
15
16
    /**
17
     * @var string Email address of users to CC
18
     */
19
    public $cc;
20
21
    /**
22
     * @var Mailable The mailable to send to the recipient
23
     */
24
    public $mailable;
25
26
    /**
27
     * Create a new job instance.
28
     *
29
     * @param  string  $to
30
     * @param  Mailable  $mailable
31
     * @param  array|null  $cc
32
     */
33
    public function __construct(string $to, Mailable $mailable, array $cc = null)
34
    {
35
        $this->to = $to;
36
        $this->mailable = $mailable;
37
        $this->cc = $cc;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cc can also be of type array. However, the property $cc is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
38
39
        $this->onQueue(config('post-office.queue'));
40
        $this->onConnection(config('post-office.driver'));
41
    }
42
43
    /**
44
     * Execute the job.
45
     *
46
     * @return bool
47
     */
48
    public function handle(): bool
49
    {
50
        $sent = $this->send();
51
52
        // Fail the Job if the mailable was not sent
53
        if (! $sent) {
54
            $this->fail();
55
56
            return false;
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * Send a Mailable to an email recipient with optional CC recipients.
64
     *
65
     * @return bool
66
     */
67
    private function send(): bool
68
    {
69
        // Initialize
70
        $mail = Mail::to($this->to);
71
72
        // CC users if provided
73
        if (isset($this->cc)) {
74
            $mail->cc($this->cc);
75
        }
76
77
        // Send mail
78
        $mail->send($this->mailable);
79
80
        // Confirm Email was sent
81
        return Mail::hasSent($this->mailable);
0 ignored issues
show
$this->mailable of type Illuminate\Mail\Mailable is incompatible with the type string expected by parameter $mailable of Illuminate\Support\Facades\Mail::hasSent(). ( Ignorable by Annotation )

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

81
        return Mail::hasSent(/** @scrutinizer ignore-type */ $this->mailable);
Loading history...
82
    }
83
}
84