Passed
Push — master ( 6e5c09...479078 )
by Stephen
02:21 queued 10s
created

SendMail::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\PostOffice\MailCenter;
4
5
use Illuminate\Mail\Mailable;
6
use Illuminate\Support\Facades\Mail;
7
use Sfneal\Queueables\AbstractJob;
8
9
10
class SendMail extends AbstractJob
11
{
12
    /**
13
     * @var string Queue to use
14
     */
15
    public $queue = 'mail';
16
17
    /**
18
     * @var string Queue connection to use
19
     */
20
    public $connection = 'database';
21
22
    /**
23
     * @var string Email address of recipient
24
     */
25
    public $to;
26
27
    /**
28
     * @var string Email address of users to CC
29
     */
30
    public $cc;
31
32
    /**
33
     * @var Mailable The mailable to send to the recipient
34
     */
35
    public $mailable;
36
37
    /**
38
     * Create a new job instance.
39
     *
40
     * @param string $to
41
     * @param Mailable $mailable
42
     * @param array|null $cc
43
     */
44
    public function __construct(string $to, Mailable $mailable, array $cc = null)
45
    {
46
        $this->to = $to;
47
        $this->mailable = $mailable;
48
        $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...
49
    }
50
51
    /**
52
     * Execute the job.
53
     *
54
     * @return void
55
     */
56
    public function handle()
57
    {
58
        if (!$this->send()) {
59
            $this->fail();
60
        }
61
    }
62
63
    /**
64
     * Send a Mailable to an email recipient with optional CC recipients
65
     *
66
     * @return bool
67
     */
68
    private function send()
69
    {
70
        // Initialize
71
        $mail = Mail::to($this->to);
72
73
        // CC users if provided
74
        if (isset($this->cc)) {
75
            $mail->cc($this->cc);
76
        }
77
78
        // Send mail
79
        $mail->send($this->mailable);
80
81
        // Confirm Email was sent
82
        return empty(Mail::failures());
83
    }
84
}
85