Passed
Push — master ( 871cbe...1ad68b )
by Gabriel
05:12
created

EmailTrait   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Test Coverage

Coverage 4%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 26
eloc 58
c 5
b 0
f 1
dl 0
loc 179
ccs 3
cts 75
cp 0.04
rs 10

15 Methods

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

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

168
            /** @scrutinizer ignore-call */ 
169
            $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...
169
            foreach ($activities as $activity) {
170
                $actEmail[$activity->email][] = $activity;
171
            }
172
            $this->getRegistry()->set('activities-email', $actEmail);
173
        }
174
175
        return $this->getRegistry()->get('activities-email');
176
    }
177
178
    /**
179
     * @return mixed
180
     */
181
    public function getLinksByEmail()
182
    {
183
        if (!$this->getRegistry()->exists('links-email')) {
184
            $linksEmail = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $linksEmail is dead and can be removed.
Loading history...
185
            $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

185
            /** @scrutinizer ignore-call */ 
186
            $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...
186
            foreach ($links as $link) {
187
                $actEmail[$link->url][$link->email] = $link;
188
            }
189
            $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 186. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
190
        }
191
192
        return $this->getRegistry()->get('links-email');
193
    }
194
195
    /**
196
     * @param Mailer $mailer
197
     * @param Message $message
198
     * @param $response
199
     */
200
    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

200
    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

200
    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...
201
    {
202
        if ($response > 0) {
203
            $this->sent = 'yes';
204
            $this->smtp_user = '';
205
            $this->smtp_host = '';
206
            $this->smtp_password = '';
207
//            $this->subject = '';
208
//            $this->body = '';
209
            //        $this->vars = '';
210
            $this->date_sent = date('Y-m-d H:i:s');
211
            $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

211
            $this->/** @scrutinizer ignore-call */ 
212
                   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...
212
213
            $this->clearAttachments();
214
        }
215
    }
216
}
217