Passed
Push — master ( 4979d9...6406dc )
by Gabriel
03:23
created

EmailTrait::IsHTML()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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

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

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

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

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

207
    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...
208
    {
209
        if ($response > 0) {
210
            $this->sent = 'yes';
211
            $this->smtp_user = '';
212
            $this->smtp_host = '';
213
            $this->smtp_password = '';
214
//            $this->subject = '';
215
//            $this->body = '';
216
            //        $this->vars = '';
217
            $this->date_sent = date('Y-m-d H:i:s');
218
            $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

218
            $this->/** @scrutinizer ignore-call */ 
219
                   update();
Loading history...
219
220
            $this->clearAttachments();
221
        }
222
    }
223
}
224