Completed
Pull Request — master (#1)
by
unknown
02:02
created

Mail::overrideTo()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Distilleries\MailerSaver\Helpers;
4
5
use Swift_Mailer;
6
use Illuminate\Mail\Mailer;
7
use Illuminate\Events\Dispatcher;
8
use Wpb\String_Blade_Compiler\Factory;
9
use Distilleries\MailerSaver\Contracts\MailModelContract;
10
use Illuminate\Contracts\Mail\Mailable as MailableContract;
11
12
class Mail extends Mailer
13
{
14
    /**
15
     * Mail model instance.
16 8
     *
17
     * @var \Distilleries\MailerSaver\Contracts\MailModelContract
18 8
     */
19 8
    protected $model;
20 8
21
    /**
22 8
     * Configuration override data.
23
     *
24
     * @var array
25
     */
26
    protected $override;
27
28
    /**
29
     * Mailersaver instance constructor.
30
     * 
31 8
     * @param  \Distilleries\MailerSaver\Contracts\MailModelContract  $model
32 8
     * @param  \Wpb\String_Blade_Compiler\Factory  $views
33 8
     * @param  \Swift_Mailer  $swift
34 8
     * @param  \Illuminate\Events\Dispatcher  $events
35
     */
36 8
    public function __construct(MailModelContract $model, Factory $views, Swift_Mailer $swift, Dispatcher $events = null)
37
    {
38 8
        $this->model = $model;
39 8
40 8
        $this->override = config('mailersaver.override');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('mailersaver.override') of type * is incompatible with the declared type array of property $override.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
42 8
        parent::__construct($views, $swift, $events);
43 8
    }
44
45 8
    /**
46
     * Render the given mail view.
47 8
     *
48
     * @param  string  $view
49 8
     * @param  array  $data
50 8
     * @return string
51
     */
52 8
    protected function getView($view, $data)
53 8
    {
54
        $body = $this->model->getTemplate($view);
55
        $body = empty($body) ? $this->views->make($view, $data)->render() : $body;
56 8
57
        $subjectTemplate = $this->model->getSubject();
58 8
        if (! empty($subjectTemplate)) {
59 8
            $subject = view([
0 ignored issues
show
Documentation introduced by
array('template' => $sub...d(), 'updated_at' => 0) is of type array<string,string|inte...updated_at":"integer"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
                'template' => $subjectTemplate,
61 8
                'cache_key' => uniqid() . rand(),
62
                'updated_at' => 0,
63 8
            ], $data);
64
            $data['subject'] = $subject->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
65 8
        } else {
66 8
            $data['subject'] = $subjectTemplate;
67
        }
68 8
69 8
        $data['body_mail'] = view([
0 ignored issues
show
Documentation introduced by
array('template' => $bod...d(), 'updated_at' => 0) is of type array<string,?,{"templat...updated_at":"integer"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
            'template' => $body,
71 8
            'cache_key' => uniqid(),
72
            'updated_at' => 0,
73
        ], $data);
74
75
        return $this->views->make(config('mailersaver.template'), $data)->render();
76
    }
77
78 8
    /**
79
     * Send contained mail instance.
80
     *
81 8
     * @param  string  $view
82 8
     * @param  array  $data
83 8
     * @param  mixed  $callback
84
     * @return void
85 8
     */
86 8
    public function send($view, array $data = [], $callback = null)
87 8
    {
88 8
        if ($view instanceof MailableContract) {
89
            return $view->send($this);
90 8
        }
91
92 8
        // First we need to parse the view, which could either be a string or an array
93
        // containing both an HTML and plain text versions of the view which should
94
        // be used when sending an e-mail. We will extract both of them out here.
95
        list($view, $plain, $raw) = $this->parseView($view);
96
97 8
        $model = $this->model->initByTemplate($view);
98 8
        $template = $model->where('action', $view)->first();
99 8
        $plain = ! empty($template) ? $template->getPlain() : $plain;
100 8
101 8
        if (! empty($template)) {
102
            $this->model = $template;
0 ignored issues
show
Documentation Bug introduced by
It seems like $template of type object<stdClass> or array is incompatible with the declared type object<Distilleries\Mail...acts\MailModelContract> of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
103 8
        }
104
105 8
        $data['message'] = $message = $this->createMessage();
106
107 8
        // Once we have retrieved the view content for the e-mail we will set the body
108
        // of this message using the HTML type, which will provide a simple wrapper
109
        // to creating view based emails that are able to receive arrays of data.
110
        $this->addContent($message, $view, $plain, $raw, $data);
111
112 8
        $this->callMessageBuilder($callback, $message);
113
        
114
        $this->addSubject($message);
115 8
        $this->addCc($message);
116
        $this->addBcc($message);
117 8
        $this->overrideTo($message);
118 8
119 4
        $this->sendSwiftMessage($message->getSwiftMessage());
120 4
    }
121
122 8
    /**
123
     * Set subject to message.
124
     *
125
     * @param  \Illuminate\Mail\Message  $message
126
     * @return void
127 8
     */
128
    public function addSubject($message)
129 8
    {
130
        $subject = $this->model->getSubject();
131 8
        if (! empty($subject)) {
132 8
            $message->setSubject($subject);
0 ignored issues
show
Bug introduced by
The method setSubject() does not exist on Illuminate\Mail\Message. Did you maybe mean subject()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
133 4
        }
134 4
    }
135
136 8
    /**
137
     * Set CC addresses to message.
138
     *
139
     * @param  \Illuminate\Mail\Message  $message
140
     * @return void
141 8
     */
142
    public function addCc($message)
143 8
    {
144
        $cc = $this->isOverride() ? $this->override['cc'] : (! empty($this->model) ? $this->model->getCc() : '');
145 8
        if (! empty($cc)) {
146 8
            $message->setCc($cc);
147 8
        }
148 8
    }
149
150 8
    /**
151
     * Set BCC addresses to message.
152 8
     *
153
     * @param  \Illuminate\Mail\Message  $message
154 8
     * @return void
155 8
     */
156
    public function addBcc($message)
157 8
    {
158
        $bcc = $this->isOverride() ? $this->override['bcc'] : $this->model->getBcc();
159
        if (! empty($bcc)) {
160
            $message->setBcc($bcc);
161
        }
162
    }
163 8
164
    /**
165 8
     * Set TO addresses to message.
166 8
     *
167 8
     * @param  \Illuminate\Mail\Message  $message
168 4
     * @return void
169 4
     */
170 8
    public function overrideTo($message)
171
    {
172
        $to = $this->isOverride() ? $this->override['to'] : '';
173
        if (! empty($to)) {
174
            $message->setTo($to);
175
        }
176
    }
177
178
    /**
179
     * Return if mail configuration is supercharged.
180
     *
181
     * @return bool
182
     */
183
    public function isOverride()
184
    {
185
        return (bool) $this->override['enabled'];
186
    }
187
}