Mail::addCc()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 4
nc 8
nop 1
crap 4
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
     *
17
     * @var \Distilleries\MailerSaver\Contracts\MailModelContract
18
     */
19
    protected $model;
20
21
    /**
22
     * Configuration override data.
23
     *
24
     * @var array
25
     */
26
    protected $override;
27
28
    /**
29
     * Mailersaver instance constructor.
30
     * 
31
     * @param  \Distilleries\MailerSaver\Contracts\MailModelContract  $model
32
     * @param  \Wpb\String_Blade_Compiler\Factory  $views
33
     * @param  \Swift_Mailer  $swift
34
     * @param  \Illuminate\Events\Dispatcher  $events
35
     */
36 12
    public function __construct(MailModelContract $model, Factory $views, Swift_Mailer $swift, Dispatcher $events = null)
37
    {
38 12
        $this->model = $model;
39
40 12
        $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 12
        parent::__construct($views, $swift, $events);
43
    }
44
45
    /**
46
     * Render the given mail view.
47
     *
48
     * @param  string $view
49
     * @param  array  $data
50
     * @return string
51
     * @throws \Throwable
52
     */
53 12
    protected function renderView($view, $data)
54
    {
55 12
        $body = $this->model->getTemplate($view);
56 12
        $body = empty($body) ? $this->views->make($view, $data)->render() : $body;
57
58 12
        $subjectTemplate = $this->model->getSubject();
59 12
        if (! empty($subjectTemplate)) {
60 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...
61 8
                'template' => $subjectTemplate,
62 8
                'cache_key' => uniqid() . rand(),
63 8
                'updated_at' => 0,
64 8
            ], $data);
65 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...
66
        } else {
67 12
            $data['subject'] = $subjectTemplate;
68
        }
69
70 12
        $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...
71 12
            'template' => $body,
72 12
            'cache_key' => uniqid(),
73 12
            'updated_at' => 0,
74 12
        ], $data);
75
76 12
        return $this->views->make(config('mailersaver.template'), $data)->render();
77
    }
78
79
    /**
80
     * Send contained mail instance.
81
     *
82
     * @param  string  $view
83
     * @param  array  $data
84
     * @param  mixed  $callback
85
     * @return void
86
     */
87 12
    public function send($view, array $data = [], $callback = null)
88
    {
89 12
        if ($view instanceof MailableContract) {
90
            return $this->sendMailable($view);
91
        }
92
93
        // First we need to parse the view, which could either be a string or an array
94
        // containing both an HTML and plain text versions of the view which should
95
        // be used when sending an e-mail. We will extract both of them out here.
96 12
        [$view, $plain, $raw] = $this->parseView($view);
0 ignored issues
show
Bug introduced by
The variable $plain seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $raw does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
97
98 12
        $model = $this->model->initByTemplate($view);
99 12
        $template = $model->where('action', $view)->first();
100 12
        $plain = ! empty($template) ? $template->getPlain() : $plain;
0 ignored issues
show
Bug introduced by
The variable $plain seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
101
102 12
        if (! empty($template)) {
103 8
            $this->model = $template;
104
        }
105
106 12
        $data['message'] = $message = $this->createMessage();
107
108
        // Once we have retrieved the view content for the e-mail we will set the body
109
        // of this message using the HTML type, which will provide a simple wrapper
110
        // to creating view based emails that are able to receive arrays of data.
111 12
        call_user_func($callback, $message);
112
113 12
        $this->addContent($message, $view, $plain, $raw, $data);
114
        
115 12
        $this->addSubject($message);
116 12
        $this->addCc($message);
117 12
        $this->addBcc($message);
118 12
        $this->overrideTo($message);
119
120
        // Next we will determine if the message should be sent. We give the developer
121
        // one final chance to stop this message and then we will send it to all of
122
        // its recipients. We will then fire the sent event for the sent message.
123 12
        $swiftMessage = $message->getSwiftMessage();
124
125 12
        if ($this->shouldSendMessage($swiftMessage, $data)) {
126 12
            $this->sendSwiftMessage($swiftMessage);
127
128 12
            $this->dispatchSentEvent($message, $data);
129
        }
130
    }
131
132
    /**
133
     * Set subject to message.
134
     *
135
     * @param  \Illuminate\Mail\Message  $message
136
     * @return void
137
     */
138 12
    public function addSubject($message)
139
    {
140 12
        $subject = $this->model->getSubject();
141 12
        if (! empty($subject)) {
142 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...
143
        }
144
    }
145
146
    /**
147
     * Set CC addresses to message.
148
     *
149
     * @param  \Illuminate\Mail\Message  $message
150
     * @return void
151
     */
152 12
    public function addCc($message)
153
    {
154 12
        $cc = $this->isOverride() ? $this->override['cc'] : (! empty($this->model) ? $this->model->getCc() : '');
155 12
        if (! empty($cc)) {
156 4
            $message->setCc($cc);
157
        }
158
    }
159
160
    /**
161
     * Set BCC addresses to message.
162
     *
163
     * @param  \Illuminate\Mail\Message  $message
164
     * @return void
165
     */
166 12
    public function addBcc($message)
167
    {
168 12
        $bcc = $this->isOverride() ? $this->override['bcc'] : $this->model->getBcc();
169 12
        if (! empty($bcc)) {
170 4
            $message->setBcc($bcc);
171
        }
172
    }
173
174
    /**
175
     * Set TO addresses to message.
176
     *
177
     * @param  \Illuminate\Mail\Message  $message
178
     * @return void
179
     */
180 12
    public function overrideTo($message)
181
    {
182 12
        $to = $this->isOverride() ? explode(',', $this->override['to']) : [];
183 12
        if (! empty($to)) {
184 8
            $message->setTo($to);
185
        }
186
    }
187
188
    /**
189
     * Return if mail configuration is supercharged.
190
     *
191
     * @return bool
192
     */
193 12
    public function isOverride()
194
    {
195 12
        return (bool) $this->override['enabled'];
196
    }
197
}