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

Mail::isOverride()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
11
class Mail extends Mailer
12
{
13
    /**
14
     * Mail model instance.
15
     *
16 8
     * @var \Distilleries\MailerSaver\Contracts\MailModelContract
17
     */
18 8
    protected $model;
19 8
20 8
    /**
21
     * Configuration override data.
22 8
     *
23
     * @var array
24
     */
25
    protected $override;
26
27
    /**
28
     * Mailersaver instance constructor.
29
     * 
30
     * @param  \Distilleries\MailerSaver\Contracts\MailModelContract  $model
31 8
     * @param  \Wpb\String_Blade_Compiler\Factory  $views
32 8
     * @param  \Swift_Mailer  $swift
33 8
     * @param  \Illuminate\Events\Dispatcher  $events
34 8
     */
35
    public function __construct(MailModelContract $model, Factory $views, Swift_Mailer $swift, Dispatcher $events = null)
36 8
    {
37
        $this->model = $model;
38 8
39 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...
40 8
41
        parent::__construct($views, $swift, $events);
42 8
    }
43 8
44
    /**
45 8
     * Render the given mail view.
46
     *
47 8
     * @param  string  $view
48
     * @param  array  $data
49 8
     * @return string
50 8
     */
51
    protected function getView($view, $data)
52 8
    {
53 8
        $body = $this->model->getTemplate($view);
54
        $body = empty($body) ? $this->views->make($view, $data)->render() : $body;
55
56 8
        $subjectTemplate = $this->model->getSubject();
57
        if (! empty($subjectTemplate)) {
58 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...
59 8
                'template' => $subjectTemplate,
60
                'cache_key' => uniqid() . rand(),
61 8
                'updated_at' => 0,
62
            ], $data);
63 8
            $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...
64
        } else {
65 8
            $data['subject'] = $subjectTemplate;
66 8
        }
67
68 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...
69 8
            'template' => $body,
70
            'cache_key' => uniqid(),
71 8
            'updated_at' => 0,
72
        ], $data);
73
74
        return $this->views->make(config('mailersaver.template'), $data)->render();
75
    }
76
77
    /**
78 8
     * Send contained mail instance.
79
     *
80
     * @param  string  $view
81 8
     * @param  array  $data
82 8
     * @param  mixed  $callback
83 8
     * @return void
84
     */
85 8
    public function send($view, array $data = [], $callback = null)
86 8
    {
87 8
        // First we need to parse the view, which could either be a string or an array
88 8
        // containing both an HTML and plain text versions of the view which should
89
        // be used when sending an e-mail. We will extract both of them out here.
90 8
        list($view, $plain, $raw) = $this->parseView($view);
91
92 8
        $model = $this->model->initByTemplate($view);
93
        $template = $model->where('action', $view)->first();
94
        $plain = ! empty($template) ? $template->getPlain() : $plain;
95
96
        if (! empty($template)) {
97 8
            $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...
98 8
        }
99 8
100 8
        $data['message'] = $message = $this->createMessage();
101 8
102
        $this->callMessageBuilder($callback, $message);
103 8
104
        // Once we have retrieved the view content for the e-mail we will set the body
105 8
        // of this message using the HTML type, which will provide a simple wrapper
106
        // to creating view based emails that are able to receive arrays of data.
107 8
        $this->addContent($message, $view, $plain, $raw, $data);
108
        $this->addSubject($message);
109
        $this->addCc($message);
110
        $this->addBcc($message);
111
        $this->overrideTo($message);
112 8
113
        $this->sendSwiftMessage($message->getSwiftMessage());
114
    }
115 8
116
    /**
117 8
     * Set subject to message.
118 8
     *
119 4
     * @param  \Illuminate\Mail\Message  $message
120 4
     * @return void
121
     */
122 8
    public function addSubject($message)
123
    {
124
        $subject = $this->model->getSubject();
125
        if (! empty($subject)) {
126
            $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...
127 8
        }
128
    }
129 8
130
    /**
131 8
     * Set CC addresses to message.
132 8
     *
133 4
     * @param  \Illuminate\Mail\Message  $message
134 4
     * @return void
135
     */
136 8
    public function addCc($message)
137
    {
138
        $cc = $this->isOverride() ? $this->override['cc'] : (! empty($this->model) ? $this->model->getCc() : '');
139
        if (! empty($cc)) {
140
            $message->setCc($cc);
141 8
        }
142
    }
143 8
144
    /**
145 8
     * Set BCC addresses to message.
146 8
     *
147 8
     * @param  \Illuminate\Mail\Message  $message
148 8
     * @return void
149
     */
150 8
    public function addBcc($message)
151
    {
152 8
        $bcc = $this->isOverride() ? $this->override['bcc'] : $this->model->getBcc();
153
        if (! empty($bcc)) {
154 8
            $message->setBcc($bcc);
155 8
        }
156
    }
157 8
158
    /**
159
     * Set TO addresses to message.
160
     *
161
     * @param  \Illuminate\Mail\Message  $message
162
     * @return void
163 8
     */
164
    public function overrideTo($message)
165 8
    {
166 8
        $to = $this->isOverride() ? $this->override['to'] : '';
167 8
        if (! empty($to)) {
168 4
            $message->setTo($to);
169 4
        }
170 8
    }
171
172
    /**
173
     * Return if mail configuration is supercharged.
174
     *
175
     * @return bool
176
     */
177
    public function isOverride()
178
    {
179
        return (bool) $this->override['enabled'];
180
    }
181
}