Passed
Push — master ( cc2322...994fe7 )
by Gabriel
03:36
created

EmailTrait   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 5.33%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 27
eloc 60
c 3
b 0
f 1
dl 0
loc 188
ccs 4
cts 75
cp 0.0533
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getBody() 0 3 1
A populateFromConfig() 0 5 1
A IsHTML() 0 7 3
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 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\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 Nip_File_System;
0 ignored issues
show
Bug introduced by
The type Nip_File_System was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Swift_Attachment;
11
use Nip\Mail\Mailer;
12
use Nip\Mail\Message;
13
14
/**
15
 * Trait EmailTrait
16
 * @package Nip\Mail\Models\EmailsTable
17
 *
18
 * @property int $id_item
19
 * @property string $type
20
 * @property string $from
21
 * @property string $from_name
22
 * @property string $smtp_host
23
 * @property string $smtp_user
24
 * @property string $smtp_password
25
 * @property string $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
trait EmailTrait
37
{
38
    use MailableRecordTrait;
39
    use HasMediaTrait;
40
    use MergeTagsRecordTrait;
41
42
    public function populateFromConfig()
43
    {
44
        $config = app('config');
45
        $this->from = $config->get('mail.from.address');
46
        $this->from_name = $config->get('mail.from.address');
47
    }
48
49
    /**
50
     * @param Record $item
51
     */
52
    public function populateFromItem($item)
53
    {
54
        $this->id_item = $item->id;
55
        $this->type = inflector()->singularize($item->getManager()->getTable());
56
    }
57
58
    /**
59
     * @inheritdoc
60
     * Used to decode html entities to proper chars
61
     */
62
    public function getFrom()
63
    {
64
        return [$this->from => html_entity_decode($this->from_name, ENT_QUOTES)];
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getPreviewBody()
71
    {
72
        return $this->getBody();
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getBody()
79
    {
80
        return $this->body;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getSubject()
87
    {
88
        return $this->subject;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getTos()
95
    {
96
        $emailsTos = [];
97
        if (preg_match_all('/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', $this->to, $matches, PREG_SET_ORDER) > 0) {
98
            foreach ($matches as $m) {
99
                if (!empty($m[2])) {
100
                    $emailsTos[trim($m[2], '<>')] = html_entity_decode($m[1]);
101
                } else {
102
                    $emailsTos[$m[1]] = '';
103
                }
104
            }
105
        }
106
107
        return $emailsTos;
108
    }
109
110
    /**
111
     * @return mixed
112
     */
113 1
    public function insert()
114
    {
115 1
        $this->saveMergeTagsToDbField();
116 1
        $this->created = date('Y-m-d H:i:s');
117
118 1
        return parent::insert();
119
    }
120
121
    public function delete()
122
    {
123
        $this->clearAttachments();
124
        return parent::delete();
125
    }
126
127
    public function clearAttachments()
128
    {
129
        $file = $this->getFiles()->delete();
0 ignored issues
show
Unused Code introduced by
The assignment to $file is dead and can be removed.
Loading history...
Bug introduced by
Are you sure the assignment to $file is correct as $this->getFiles()->delete() targeting ByTIC\MediaLibrary\Colle...ns\Collection::delete() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
130
    }
131
132
133
    /**
134
     * @param Message $message
135
     */
136
    public function buildMailMessageAttachments(&$message)
137
    {
138
        $emailFiles = $this->getFiles();
139
        foreach ($emailFiles as $emailFile) {
140
            $message->attach(
141
                Swift_Attachment::newInstance($emailFile->read(), $emailFile->getName())
142
            );
143
        }
144
    }
145
146
    /**
147
     * @return array|null
148
     */
149
    protected function getCustomArgs()
150
    {
151
        $args = [];
152
        $args['category'] = $this->type;
153
        $args['id_email'] = $this->id;
154
155
        return $args;
156
    }
157
158
    /**
159
     * @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...
160
     * @return bool
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(DATE_DB);
0 ignored issues
show
Bug introduced by
The constant Nip\MailModule\Models\EmailsTable\DATE_DB was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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