Passed
Push — master ( 994fe7...40271a )
by Gabriel
01:42
created

EmailTrait   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Test Coverage

Coverage 5.26%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 27
eloc 60
c 4
b 0
f 1
dl 0
loc 189
ccs 4
cts 76
cp 0.0526
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A IsHTML() 0 7 3
A afterSend() 0 14 2
A getActivitiesByEmail() 0 12 3
A getLinksByEmail() 0 12 3
A getBody() 0 3 1
A populateFromConfig() 0 5 1
A insert() 0 6 1
A delete() 0 4 1
A getFrom() 0 3 1
A getTos() 0 14 4
A getPreviewBody() 0 3 1
A buildMailMessageAttachments() 0 6 2
A populateFromItem() 0 4 1
A clearAttachments() 0 3 1
A getCustomArgs() 0 7 1
A getSubject() 0 3 1
1
<?php
2
3
namespace Nip\MailModule\Models\EmailsTable;
4
5
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
6
use Nip\Mail\Models\Mailable\RecordTrait as MailableRecordTrait;
7
use Nip\MailModule\Models\EmailsTable\Traits\MergeTags\MergeTagsRecordTrait;
8
use Nip\Records\AbstractModels\Record;
9
use Swift_Attachment;
10
use Nip\Mail\Mailer;
11
use Nip\Mail\Message;
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 $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
{
37
    use MailableRecordTrait;
38
    use HasMediaTrait;
39
    use MergeTagsRecordTrait;
40
41
    public function populateFromConfig()
42
    {
43
        $config = app('config');
44
        $this->from = $config->get('mail.from.address');
45
        $this->from_name = $config->get('mail.from.address');
46
    }
47
48
    /**
49
     * @param Record $item
50
     */
51
    public function populateFromItem($item)
52
    {
53
        $this->id_item = $item->id;
54
        $this->type = inflector()->singularize($item->getManager()->getTable());
55
    }
56
57
    /**
58
     * @inheritdoc
59
     * Used to decode html entities to proper chars
60
     */
61
    public function getFrom()
62
    {
63
        return [$this->from => html_entity_decode($this->from_name, ENT_QUOTES)];
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getPreviewBody()
70
    {
71
        return $this->getBody();
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getBody()
78
    {
79
        return $this->body;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getSubject()
86
    {
87
        return $this->subject;
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getTos()
94
    {
95
        $emailsTos = [];
96
        if (preg_match_all('/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', $this->to, $matches, PREG_SET_ORDER) > 0) {
97
            foreach ($matches as $m) {
98
                if (!empty($m[2])) {
99
                    $emailsTos[trim($m[2], '<>')] = html_entity_decode($m[1]);
100
                } else {
101
                    $emailsTos[$m[1]] = '';
102
                }
103
            }
104
        }
105
106
        return $emailsTos;
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112 1
    public function insert()
113
    {
114 1
        $this->saveMergeTagsToDbField();
115 1
        $this->created = date('Y-m-d H:i:s');
116
117 1
        return parent::insert();
118
    }
119
120
    public function delete()
121
    {
122
        $this->clearAttachments();
123
        parent::delete();
124
    }
125
126
    public function clearAttachments()
127
    {
128
        $this->getFiles()->delete();
129
    }
130
131
    /**
132
     * @param Message $message
133
     */
134
    public function buildMailMessageAttachments(&$message)
135
    {
136
        $emailFiles = $this->getFiles();
137
        foreach ($emailFiles as $emailFile) {
138
            $message->attach(
139
                Swift_Attachment::newInstance($emailFile->read(), $emailFile->getName())
140
            );
141
        }
142
    }
143
144
    /**
145
     * @return array|null
146
     */
147
    protected function getCustomArgs()
148
    {
149
        $args = [];
150
        $args['category'] = $this->type;
151
        $args['id_email'] = $this->id;
152
153
        return $args;
154
    }
155
156
    /**
157
     * @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...
158
     * @return bool
159
     * @noinspection PhpUnused
160
     * @noinspection PhpMethodNamingConventionInspection
161
     */
162
    public function IsHTML($value = null)
163
    {
164
        if (is_bool($value)) {
0 ignored issues
show
introduced by
The condition is_bool($value) is always false.
Loading history...
165
            $this->is_html = $value ? 'yes' : 'no';
166
        }
167
168
        return $this->is_html == 'yes';
169
    }
170
171
    /**
172
     * @return mixed
173
     */
174
    public function getActivitiesByEmail()
175
    {
176
        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

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

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

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

210
    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

210
    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...
211
    {
212
        if ($response > 0) {
213
            $this->sent = 'yes';
214
            $this->smtp_user = '';
215
            $this->smtp_host = '';
216
            $this->smtp_password = '';
217
//            $this->subject = '';
218
//            $this->body = '';
219
            //        $this->vars = '';
220
            $this->date_sent = date('Y-m-d H:i:s');
221
            $this->update();
0 ignored issues
show
Bug introduced by
It seems like update() 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

221
            $this->/** @scrutinizer ignore-call */ 
222
                   update();
Loading history...
222
223
            $this->clearAttachments();
224
        }
225
    }
226
}
227