Passed
Push — master ( ad153a...90e717 )
by Stephen
58s queued 10s
created

AbstractMailable::setView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\PostOffice\Mailables;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Mail\Mailable as BaseMailable;
7
use Illuminate\Queue\SerializesModels;
8
9
abstract class AbstractMailable extends BaseMailable
10
{
11
    // todo: refactor to allow interfaces to set properties without passing to constructor
12
    // todo: refactor to use basic mailable building instead of views (new class?)
13
    // todo: add use of config for setting default view
14
    use Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by Sfneal\PostOffice\Mailables\AbstractMailable: $id, $relations, $class, $keyBy
Loading history...
15
16
    /**
17
     * @var string View blade to use for mailable content
18
     */
19
    public $view;
20
21
    /**
22
     * @var string First line of the email body
23
     */
24
    public $greeting;
25
26
    /**
27
     * @var string Recipient email address
28
     */
29
    public $email;
30
31
    /**
32
     * @var string Subject of the email
33
     */
34
    public $title;
35
36
    /**
37
     * @var array|string Array of strings to be displayed as paragraphs
38
     */
39
    public $messages;
40
41
    /**
42
     * @var array Action button url and text
43
     */
44
    public $call_to_action;
45
46
    /**
47
     * Mailable constructor.
48
     *
49
     * @param string|null $greeting
50
     * @param string|null $email
51
     * @param string|null $title
52
     * @param null $messages
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $messages is correct as it would always require null to be passed?
Loading history...
53
     * @param array|null $call_to_action
54
     * @param string|null $view
55
     */
56
    public function __construct(string $greeting = null,
57
                                string $email = null,
58
                                string $title = null,
59
                                $messages = null,
60
                                array $call_to_action = null,
61
                                string $view = null)
62
    {
63
        $this->greeting = $greeting ?? $this->greeting;
64
        $this->email = $email ?? $this->email;
65
        $this->title = $title ?? $this->title;
66
        $this->messages = $messages ?? $this->messages;
67
        $this->call_to_action = $call_to_action ?? $this->call_to_action;
68
69
        $this->view = $this->setView($view);
70
        $this->onQueue(config('post-office.queue'));
71
        $this->onConnection(config('post-office.driver'));
72
    }
73
74
    /**
75
     * Build the message.
76
     *
77
     * @return $this
78
     */
79
    public function build(): self
80
    {
81
        return $this->subject($this->title)
82
            ->view($this->view, [
83
                'greeting' => $this->greeting,
84
                'email' => $this->email,
85
                'title' => $this->title,
86
                'messages' => $this->messages,
87
                'call_to_action' => $this->call_to_action,
88
            ]);
89
    }
90
91
    /**
92
     * Set the Mailable's $view property.
93
     *
94
     *  1. $view parameter passed to the constructor
95
     *  2. $view property declared in child `AbstractMailable` extension
96
     *  3. 'post-office.mailable.view' config value (defaults to post-office::email)
97
     *
98
     * @param string|null $view
99
     * @return string
100
     */
101
    private function setView(string $view = null): string
102
    {
103
        return $view ?? $this->view ?? config('post-office.mailables.view');
104
    }
105
}
106