Passed
Push — master ( a11996...1d95ca )
by Gabriel
13:54
created

EmailTrait::getBccTos()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Nip\MailModule\Models\EmailsTable;
4
5
use ByTIC\DataObjects\Behaviors\Timestampable\TimestampableTrait;
6
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
7
use Nip\Mail\Mailer;
8
use Nip\Mail\Message;
9
use Nip\Mail\Models\Mailable\RecordTrait as MailableRecordTrait;
10
use Nip\MailModule\Models\EmailsTable\Traits\MergeTags\MergeTagsRecordTrait;
11
use Nip\Records\AbstractModels\Record;
12
13
/**
14
 * Trait EmailTrait
15
 * @package Nip\Mail\Models\EmailsTable
16
 *
17
 * @property int $id_item
18
 * @property string $type
19
 * @property string $smtp_host
20
 * @property string $smtp_user
21
 * @property string $smtp_password
22
 * @property string $to
23
 * @property string $reply_to
24
 * @property string $bcc
25
 * @property string $subject
26
 * @property string $compiled_subject
27
 * @property string $body
28
 * @property string $compiled_body
29
 * @property string $vars
30
 * @property string $is_html
31
 * @property string $sent
32
 * @property string $date_sent
33
 * @property string $created
34
 */
35
trait EmailTrait
36 1
{
37 1
    use MailableRecordTrait;
38 1
    use HasMediaTrait;
39
    use MergeTagsRecordTrait;
40
    use TimestampableTrait;
41
42
    public ?string $from = '';
43
    public ?string $from_name = '';
44
45
    public ?string $subject = '';
46
47
    protected static $createTimestamps = 'created';
48
49
    public function populateFromConfig()
50
    {
51
        $config = app('config');
52
        $this->from = $config->get('mail.from.address');
53
        $this->from_name = $config->get('mail.from.name');
54
    }
55
56
    /**
57
     * @param Record $item
58
     */
59
    public function populateFromItem($item)
60
    {
61
        $this->id_item = $item->id;
62
        $this->type = inflector()->singularize($item->getManager()->getTable());
63
    }
64
65
    /**
66
     * @inheritdoc
67
     * Used to decode html entities to proper chars
68
     */
69
    public function getFrom()
70
    {
71
        if (empty($this->getPropertyRaw('from'))) {
0 ignored issues
show
Bug introduced by
It seems like getPropertyRaw() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        if (empty($this->/** @scrutinizer ignore-call */ getPropertyRaw('from'))) {
Loading history...
72
            $config = app('config');
73
            $this->from = $config->get('mail.from.address');
74
            $this->from_name = $config->get('mail.from.name');
75
        }
76
        return [$this->from => html_entity_decode($this->from_name, ENT_QUOTES)];
0 ignored issues
show
Bug introduced by
It seems like $this->from_name can also be of type null; however, parameter $string of html_entity_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        return [$this->from => html_entity_decode(/** @scrutinizer ignore-type */ $this->from_name, ENT_QUOTES)];
Loading history...
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getPreviewBody()
83
    {
84
        return $this->getBody();
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getBody()
91
    {
92
        return $this->body;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getSubject()
99
    {
100
        return $this->subject;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getTos()
107
    {
108
        $emailsTos = [];
109
        if (preg_match_all('/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', $this->to, $matches, PREG_SET_ORDER) > 0) {
110
            foreach ($matches as $m) {
111
                if (!empty($m[2])) {
112
                    $emailsTos[trim($m[2], '<>')] = html_entity_decode($m[1]);
113
                } else {
114
                    $emailsTos[$m[1]] = '';
115
                }
116
            }
117
        }
118
119
        return $emailsTos;
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function getReplyTos()
126
    {
127
        if (empty($this->reply_to)) {
128
            return;
129
        }
130
        return [$this->reply_to => html_entity_decode($this->from_name, ENT_QUOTES)];
0 ignored issues
show
Bug introduced by
It seems like $this->from_name can also be of type null; however, parameter $string of html_entity_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
        return [$this->reply_to => html_entity_decode(/** @scrutinizer ignore-type */ $this->from_name, ENT_QUOTES)];
Loading history...
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    public function getBccTos()
137
    {
138
        if (empty($this->bcc)) {
139
            return;
140
        }
141
        return [$this->bcc => ''];
142
    }
143
144
    public function delete()
145
    {
146
        $this->clearAttachments();
147
        parent::delete();
148
    }
149
150
    public function clearAttachments()
151
    {
152
        $this->getFiles()->delete();
153
    }
154
155
    /**
156
     * @param Message $message
157
     */
158
    public function buildMailMessageAttachments(&$message)
159
    {
160
        $emailFiles = $this->getFiles();
161
        foreach ($emailFiles as $emailFile) {
162
            $message->attachFromContent($emailFile->read(), $emailFile->getName());
163
        }
164
    }
165
166
    /**
167
     * @return array|null
168
     */
169
    protected function getCustomArgs(): ?array
170
    {
171
        $args = [];
172
        $args['category'] = $this->type;
173
        $args['id_email'] = $this->id;
174
175
        return $args;
176
    }
177
178
    /**
179
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
180
     * @return bool
181
     * @noinspection PhpUnused
182
     * @noinspection PhpMethodNamingConventionInspection
183
     */
184
    public function IsHTML($value = null)
185
    {
186
        if (is_bool($value)) {
0 ignored issues
show
introduced by
The condition is_bool($value) is always false.
Loading history...
187
            $this->is_html = $value ? 'yes' : 'no';
188
        }
189
190
        return $this->is_html == 'yes';
191
    }
192
193
    /**
194
     * @return mixed
195
     */
196
    public function getActivitiesByEmail()
197
    {
198
        if (!$this->getRegistry()->exists('activities-email')) {
0 ignored issues
show
Bug introduced by
It seems like getRegistry() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
        if (!$this->/** @scrutinizer ignore-call */ getRegistry()->exists('activities-email')) {
Loading history...
199
            $actEmail = [];
200
            $activities = $this->getActivities();
0 ignored issues
show
Bug introduced by
The method getActivities() does not exist on Nip\MailModule\Models\EmailsTable\EmailTrait. Did you maybe mean getActivitiesByEmail()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
            /** @scrutinizer ignore-call */ 
201
            $activities = $this->getActivities();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
201
            foreach ($activities as $activity) {
202
                $actEmail[$activity->email][] = $activity;
203
            }
204
            $this->getRegistry()->set('activities-email', $actEmail);
205
        }
206
207
        return $this->getRegistry()->get('activities-email');
208
    }
209
210
    /**
211
     * @return mixed
212
     */
213
    public function getLinksByEmail()
214
    {
215
        if (!$this->getRegistry()->exists('links-email')) {
216
            $linksEmail = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $linksEmail is dead and can be removed.
Loading history...
217
            $links = $this->getLinks();
0 ignored issues
show
Bug introduced by
The method getLinks() does not exist on Nip\MailModule\Models\EmailsTable\EmailTrait. Did you maybe mean getLinksByEmail()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
            /** @scrutinizer ignore-call */ 
218
            $links = $this->getLinks();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
            foreach ($links as $link) {
219
                $actEmail[$link->url][$link->email] = $link;
220
            }
221
            $this->getRegistry()->set('links-email', $actEmail);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $actEmail seems to be defined by a foreach iteration on line 218. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
222
        }
223
224
        return $this->getRegistry()->get('links-email');
225
    }
226
227
    /**
228
     * @param Mailer $mailer
229
     * @param Message $message
230
     * @param $response
231
     */
232
    protected function afterSend($mailer, $message, $response)
0 ignored issues
show
Unused Code introduced by
The parameter $mailer is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

232
    protected function afterSend(/** @scrutinizer ignore-unused */ $mailer, $message, $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

232
    protected function afterSend($mailer, /** @scrutinizer ignore-unused */ $message, $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
233
    {
234
        if ($response > 0) {
235
            $this->sent = 'yes';
236
            $this->smtp_user = '';
237
            $this->smtp_host = '';
238
            $this->smtp_password = '';
239
//            $this->subject = '';
240
//            $this->body = '';
241
            //        $this->vars = '';
242
            $this->date_sent = date('Y-m-d H:i:s');
243
            $this->update();
0 ignored issues
show
Bug introduced by
The method update() does not exist on Nip\MailModule\Models\EmailsTable\EmailTrait. Did you maybe mean updatedTimestamps()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

243
            $this->/** @scrutinizer ignore-call */ 
244
                   update();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
244
245
            $this->clearAttachments();
246
        }
247
    }
248
}
249