Issues (39)

src/Models/EmailsTable/EmailTrait.php (14 issues)

1
<?php
2
declare(strict_types=1);
3
4
namespace Nip\MailModule\Models\EmailsTable;
5
6
use ByTIC\DataObjects\Behaviors\Timestampable\TimestampableTrait;
7
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
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
use Symfony\Component\Mailer\MailerInterface;
13
14
/**
15
 * Trait EmailTrait
16
 * @package Nip\Mail\Models\EmailsTable
17
 *
18
 * @property int $id_item
19
 * @property string $type
20
 * @property string $smtp_host
21
 * @property string $smtp_user
22
 * @property string $smtp_password
23
 * @property string $to
24
 * @property string $reply_to
25
 * @property string $bcc
26
 * @property string $subject
27
 * @property string $compiled_subject
28
 * @property string $body
29
 * @property string $compiled_body
30
 * @property string $vars
31
 * @property string $is_html
32
 * @property string $sent
33
 * @property string $date_sent
34
 * @property string $created
35
 */
36 1
trait EmailTrait
37 1
{
38 1
    use MailableRecordTrait;
39
    use HasMediaTrait;
40
    use MergeTagsRecordTrait;
41
    use TimestampableTrait;
42
43
    public ?string $body = null;
44
45
    public ?string $from = '';
46
    public ?string $from_name = '';
47
48
    public ?string $subject = '';
49
50
    protected static $createTimestamps = 'created';
51
52
    public function populateFromConfig()
53
    {
54
        $config = app('config');
55
        $this->from = $config->get('mail.from.address');
56
        $this->from_name = $config->get('mail.from.name');
57
    }
58
59
    /**
60
     * @param Record $item
61
     */
62
    public function populateFromItem($item)
63
    {
64
        $this->id_item = $item->id;
65
        $this->type = inflector()->singularize($item->getManager()->getTable());
66
    }
67
68
    /**
69
     * @inheritdoc
70
     * Used to decode html entities to proper chars
71
     */
72
    public function getFrom()
73
    {
74
        if (empty($this->getPropertyRaw('from'))) {
0 ignored issues
show
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

74
        if (empty($this->/** @scrutinizer ignore-call */ getPropertyRaw('from'))) {
Loading history...
75
            $config = app('config');
76
            $this->from = $config->get('mail.from.address');
77
            $this->from_name = $config->get('mail.from.name');
78
        }
79
        return html_entity_decode($this->from_name, ENT_QUOTES) . ' <' . $this->from . '>';
0 ignored issues
show
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

79
        return html_entity_decode(/** @scrutinizer ignore-type */ $this->from_name, ENT_QUOTES) . ' <' . $this->from . '>';
Loading history...
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getPreviewBody()
86
    {
87
        return $this->getBody();
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getBody()
94
    {
95
        return $this->body;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getSubject(): ?string
102
    {
103
        return $this->subject;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getTos()
110
    {
111
        $emailsTos = [];
112
        if (preg_match_all('/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', (string)$this->to, $matches,
113
                PREG_SET_ORDER) > 0) {
114
            foreach ($matches as $m) {
115
                if (!empty($m[2])) {
116
                    $emailsTos[trim($m[2], '<>')] = html_entity_decode($m[1]);
117
                } else {
118
                    $emailsTos[$m[1]] = '';
119
                }
120
            }
121
        }
122
123
        return $emailsTos;
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function getReplyTos()
130
    {
131
        if (empty($this->reply_to)) {
132
            return null;
133
        }
134
        return [$this->reply_to => html_entity_decode($this->from_name, ENT_QUOTES)];
0 ignored issues
show
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

134
        return [$this->reply_to => html_entity_decode(/** @scrutinizer ignore-type */ $this->from_name, ENT_QUOTES)];
Loading history...
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function getBccTos()
141
    {
142
        if (empty($this->bcc)) {
143
            return null;
144
        }
145
        return [$this->bcc => ''];
146
    }
147
148
    public function delete()
149
    {
150
        $this->clearAttachments();
151
        parent::delete();
152
    }
153
154
    public function clearAttachments()
155
    {
156
        $this->getFiles()->delete();
157
    }
158
159
    /**
160
     * @param Message $message
161
     */
162
    public function buildMailMessageAttachments(&$message)
163
    {
164
        $emailFiles = $this->getFiles();
165
        foreach ($emailFiles as $emailFile) {
166
            $message->attachFromContent($emailFile->read(), $emailFile->getName());
0 ignored issues
show
Deprecated Code introduced by
The function Nip\Mail\Message::attachFromContent() has been deprecated: use attach() instead ( Ignorable by Annotation )

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

166
            /** @scrutinizer ignore-deprecated */ $message->attachFromContent($emailFile->read(), $emailFile->getName());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
167
        }
168
    }
169
170
    /**
171
     * @return array|null
172
     */
173
    protected function getCustomArgs(): ?array
174
    {
175
        $args = [];
176
        $args['category'] = $this->type;
177
        $args['id_email'] = $this->id;
178
179
        return $args;
180
    }
181
182
    /**
183
     * @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...
184
     * @return bool
185
     * @noinspection PhpUnused
186
     * @noinspection PhpMethodNamingConventionInspection
187
     */
188
    public function IsHTML($value = null)
189
    {
190
        if (is_bool($value)) {
0 ignored issues
show
The condition is_bool($value) is always false.
Loading history...
191
            $this->is_html = $value ? 'yes' : 'no';
192
        }
193
194
        return $this->is_html == 'yes';
195
    }
196
197
    /**
198
     * @return mixed
199
     */
200
    public function getActivitiesByEmail()
201
    {
202
        if (!$this->getRegistry()->exists('activities-email')) {
0 ignored issues
show
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

202
        if (!$this->/** @scrutinizer ignore-call */ getRegistry()->exists('activities-email')) {
Loading history...
203
            $actEmail = [];
204
            $activities = $this->getActivities();
0 ignored issues
show
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

204
            /** @scrutinizer ignore-call */ 
205
            $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...
205
            foreach ($activities as $activity) {
206
                $actEmail[$activity->email][] = $activity;
207
            }
208
            $this->getRegistry()->set('activities-email', $actEmail);
209
        }
210
211
        return $this->getRegistry()->get('activities-email');
212
    }
213
214
    /**
215
     * @return mixed
216
     */
217
    public function getLinksByEmail()
218
    {
219
        if (!$this->getRegistry()->exists('links-email')) {
220
            $linksEmail = [];
0 ignored issues
show
The assignment to $linksEmail is dead and can be removed.
Loading history...
221
            $links = $this->getLinks();
0 ignored issues
show
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

221
            /** @scrutinizer ignore-call */ 
222
            $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...
222
            foreach ($links as $link) {
223
                $actEmail[$link->url][$link->email] = $link;
224
            }
225
            $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 222. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
226
        }
227
228
        return $this->getRegistry()->get('links-email');
229
    }
230
231
    /**
232
     * @param MailerInterface $mailer
233
     * @param Message $message
234
     * @param $response
235
     */
236
    protected function afterSend($mailer, $message)
0 ignored issues
show
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

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

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...
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

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

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...
237
    {
238
        $this->sent = 'yes';
239
        $this->smtp_user = '';
240
        $this->smtp_host = '';
241
        $this->smtp_password = '';
242
//            $this->subject = '';
243
//            $this->body = '';
244
        //        $this->vars = '';
245
        $this->date_sent = date('Y-m-d H:i:s');
246
        $this->update();
0 ignored issues
show
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

246
        $this->/** @scrutinizer ignore-call */ 
247
               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...
247
248
        $this->clearAttachments();
249
    }
250
}
251