Passed
Push — master ( ef4233...391ca4 )
by Gabriel
05:46
created

EmailTrait::populateFromItem()   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 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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 $from
20
 * @property string $from_name
21
 * @property string $smtp_host
22
 * @property string $smtp_user
23
 * @property string $smtp_password
24
 * @property string $to
25
 * @property string $reply_to
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
    protected static $createTimestamps = 'created';
44
45
    public function populateFromConfig()
46
    {
47
        $config = app('config');
48
        $this->from = $config->get('mail.from.address');
49
        $this->from_name = $config->get('mail.from.name');
50
    }
51
52
    /**
53
     * @param Record $item
54
     */
55
    public function populateFromItem($item)
56
    {
57
        $this->id_item = $item->id;
58
        $this->type = inflector()->singularize($item->getManager()->getTable());
59
    }
60
61
    /**
62
     * @inheritdoc
63
     * Used to decode html entities to proper chars
64
     */
65
    public function getFrom()
66
    {
67
        if (empty($this->getAttribute('from'))) {
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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

67
        if (empty($this->/** @scrutinizer ignore-call */ getAttribute('from'))) {
Loading history...
68
            $config = app('config');
69
            $this->from = $config->get('mail.from.address');
70
            $this->from_name = $config->get('mail.from.name');
71
        }
72
        return [$this->from => html_entity_decode($this->from_name, ENT_QUOTES)];
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getPreviewBody()
79
    {
80
        return $this->getBody();
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getBody()
87
    {
88
        return $this->body;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getSubject()
95
    {
96
        return $this->subject;
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getTos()
103
    {
104
        $emailsTos = [];
105
        if (preg_match_all('/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', $this->to, $matches, PREG_SET_ORDER) > 0) {
106
            foreach ($matches as $m) {
107
                if (!empty($m[2])) {
108
                    $emailsTos[trim($m[2], '<>')] = html_entity_decode($m[1]);
109
                } else {
110
                    $emailsTos[$m[1]] = '';
111
                }
112
            }
113
        }
114
115
        return $emailsTos;
116
    }
117
118
    /**
119
     * @return array
120
     */
121
    public function getReplyTos()
122
    {
123
        if (empty($this->reply_to)) {
124
            return;
125
        }
126
        return [$this->reply_to => html_entity_decode($this->from_name, ENT_QUOTES)];
127
    }
128
129
    public function delete()
130
    {
131
        $this->clearAttachments();
132
        parent::delete();
133
    }
134
135
    public function clearAttachments()
136
    {
137
        $this->getFiles()->delete();
138
    }
139
140
    /**
141
     * @param Message $message
142
     */
143
    public function buildMailMessageAttachments(&$message)
144
    {
145
        $emailFiles = $this->getFiles();
146
        foreach ($emailFiles as $emailFile) {
147
            $message->attachFromContent($emailFile->read(), $emailFile->getName());
148
        }
149
    }
150
151
    /**
152
     * @return array|null
153
     */
154
    protected function getCustomArgs(): ?array
155
    {
156
        $args = [];
157
        $args['category'] = $this->type;
158
        $args['id_email'] = $this->id;
159
160
        return $args;
161
    }
162
163
    /**
164
     * @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...
165
     * @return bool
166
     * @noinspection PhpUnused
167
     * @noinspection PhpMethodNamingConventionInspection
168
     */
169
    public function IsHTML($value = null)
170
    {
171
        if (is_bool($value)) {
0 ignored issues
show
introduced by
The condition is_bool($value) is always false.
Loading history...
172
            $this->is_html = $value ? 'yes' : 'no';
173
        }
174
175
        return $this->is_html == 'yes';
176
    }
177
178
    /**
179
     * @return mixed
180
     */
181
    public function getActivitiesByEmail()
182
    {
183
        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

183
        if (!$this->/** @scrutinizer ignore-call */ getRegistry()->exists('activities-email')) {
Loading history...
184
            $actEmail = [];
185
            $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

185
            /** @scrutinizer ignore-call */ 
186
            $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...
186
            foreach ($activities as $activity) {
187
                $actEmail[$activity->email][] = $activity;
188
            }
189
            $this->getRegistry()->set('activities-email', $actEmail);
190
        }
191
192
        return $this->getRegistry()->get('activities-email');
193
    }
194
195
    /**
196
     * @return mixed
197
     */
198
    public function getLinksByEmail()
199
    {
200
        if (!$this->getRegistry()->exists('links-email')) {
201
            $linksEmail = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $linksEmail is dead and can be removed.
Loading history...
202
            $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

202
            /** @scrutinizer ignore-call */ 
203
            $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...
203
            foreach ($links as $link) {
204
                $actEmail[$link->url][$link->email] = $link;
205
            }
206
            $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 203. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
207
        }
208
209
        return $this->getRegistry()->get('links-email');
210
    }
211
212
    /**
213
     * @param Mailer $mailer
214
     * @param Message $message
215
     * @param $response
216
     */
217
    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

217
    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

217
    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...
218
    {
219
        if ($response > 0) {
220
            $this->sent = 'yes';
221
            $this->smtp_user = '';
222
            $this->smtp_host = '';
223
            $this->smtp_password = '';
224
//            $this->subject = '';
225
//            $this->body = '';
226
            //        $this->vars = '';
227
            $this->date_sent = date('Y-m-d H:i:s');
228
            $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

228
            $this->/** @scrutinizer ignore-call */ 
229
                   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...
229
230
            $this->clearAttachments();
231
        }
232
    }
233
}
234