Passed
Push — master ( 2a8232...4f3876 )
by eXeCUT
04:33
created

Filter::filtrateByAttachmentName()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
ccs 12
cts 13
cp 0.9231
rs 8.8333
c 0
b 0
f 0
cc 7
nc 9
nop 1
crap 7.0222
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: execut
5
 * Date: 9/27/16
6
 * Time: 3:40 PM
7
 */
8
9
namespace execut\import\components\source\adapter\email;
10
11
12
use yii\base\Component;
13
14
class Filter extends Component
15
{
16
    public $subject = null;
17
    public $sender = null;
18
    public $attachmentName = null;
19
    public $excludedFileTypes = [
20
        'jpg',
21
        'jpeg',
22
        'gif',
23
        'png',
24
        'bmp'
25
    ];
26 7
    public function filtrate($mails) {
27 7
        if (empty($this->subject) && empty($this->sender) && empty($this->attachmentName)) {
28 1
            return [];
29
        }
30
31 7
        $this->filtrateBySender($mails);
32 7
        $this->filtrateBySubject($mails);
33 7
        $this->filtrateByFileTypes($mails);
34 7
        $this->filtrateByAttachmentName($mails);
35
36 7
        return $mails;
37
    }
38
39 7
    protected function filtrateByFileTypes(&$mails) {
40 7
        if ($this->excludedFileTypes === null) {
41 1
            return;
42
        }
43
44 6
        foreach ($mails as $mail) {
45 6
            if (empty($mail->attachments)) {
46 5
                continue;
47
            }
48
49 1
            $attachments = [];
50 1
            foreach ($mail->attachments as $attachment) {
51 1
                $isExcluded = false;
52 1
                foreach ($this->excludedFileTypes as $fileType) {
53 1
                    if (strpos($attachment->fileName, '.' . $fileType) !== false) {
54 1
                        $isExcluded = true;
55
                    }
56
                }
57
58 1
                if (!$isExcluded) {
59 1
                    $attachments[] = $attachment;
60
                }
61
            }
62
63 1
            $mail->attachments = $attachments;
64
        }
65 6
    }
66
67 7
    protected function filtrateByAttachmentName(&$mails) {
68 7
        if ($this->attachmentName === null) {
69 6
            return;
70
        }
71
72 1
        foreach ($mails as $key => $mail) {
73 1
            if (empty($mail->attachments)) {
74
                continue;
75
            }
76
77 1
            $attachments = [];
78 1
            foreach ($mail->attachments as $attachment) {
79 1
                if ($this->isTemplateMatched($attachment->fileName, $this->attachmentName)) {
80 1
                    $attachments[] = $attachment;
81
                }
82
            }
83
84 1
            if (empty($attachments)) {
85 1
                unset($mails[$key]);
86
            }
87 1
            $mail->attachments = $attachments;
88
        }
89 1
    }
90
91 7
    protected function filtrateBySender(&$mails) {
92 7
        return $this->filtrateByAttribute($mails, 'sender');
0 ignored issues
show
Bug introduced by eXeCUT
Are you sure the usage of $this->filtrateByAttribute($mails, 'sender') targeting execut\import\components...::filtrateByAttribute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
93
    }
94
95 7
    protected function filtrateBySubject(&$mails) {
96 7
        return $this->filtrateByAttribute($mails, 'subject');
0 ignored issues
show
Bug introduced by eXeCUT
Are you sure the usage of $this->filtrateByAttribute($mails, 'subject') targeting execut\import\components...::filtrateByAttribute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
97
    }
98
99 7
    protected function filtrateByAttribute(&$mails, $attribute) {
100 7
        if (empty($this->$attribute)) {
101 7
            return;
102
        }
103
104 6
        $subjectTemplate = $this->$attribute;
105 6
        foreach ($mails as $mailKey => $mail) {
106 6
            $subject = $mail->$attribute;
107 6
            $isMatch = $this->isTemplateMatched($subject, $subjectTemplate);
108 6
            if (!$isMatch) {
109 6
                unset($mails[$mailKey]);
110
            }
111
        }
112 6
    }
113
114
    /**
115
     * @param $subject
116
     * @param $subjectTemplate
117
     * @return bool
118
     */
119 7
    protected function isTemplateMatched($subject, $subjectTemplate)
120
    {
121 7
        $isMatch = true;
122 7
        $subject = mb_strtolower($subject);
123 7
        $subject = trim($subject);
124 7
        $subjectTemplate = mb_strtolower($subjectTemplate);
125 7
        if (strpos($subjectTemplate, '*') !== false) {
126 4
            $parts = explode('*', $subjectTemplate);
127 4
            $prevMatchPos = false;
128 4
            foreach ($parts as $key => $part) {
129 4
                if (empty($part) && $key !== 0) {
130 2
                    if ($key === (count($parts) - 1)) {
131 2
                        break;
132
                    }
133
134
                    $matchPos = 0;
135
                } else {
136 4
                    if (empty($part) && $key === 0) {
137 1
                        $matchPos = 0;
138
                    } else {
139 4
                        $matchPos = strpos($subject, $part);
140
                    }
141
142 4
                    if ($key === 0 && $matchPos !== 0) {
143 2
                        $isMatch = false;
144 2
                        break;
145
                    }
146
                }
147
148 4
                if ($matchPos === false || ($prevMatchPos !== false && $matchPos < $prevMatchPos)) {
149
                    $isMatch = false;
150
                    break;
151
                }
152
153 4
                if ($key === (count($parts) - 1) && (strlen($subject) > (strlen($part) + $matchPos))) {
154 1
                    $isMatch = false;
155 1
                    break;
156
                }
157
158 4
                $prevMatchPos = $matchPos;
159
            }
160 4
        } else if ($subject !== $subjectTemplate) {
161 2
            $isMatch = false;
162
        }
163
164 7
        return $isMatch;
165
    }
166
}