Passed
Push — master ( 662d52...2a8232 )
by eXeCUT
03:58
created

Filter::filtrateByAttachmentName()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6.027
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 $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
            $mail->attachments = $attachments;
85
        }
86 1
    }
87
88 7
    protected function filtrateBySender(&$mails) {
89 7
        return $this->filtrateByAttribute($mails, 'sender');
0 ignored issues
show
Bug introduced by
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...
90
    }
91
92 7
    protected function filtrateBySubject(&$mails) {
93 7
        return $this->filtrateByAttribute($mails, 'subject');
0 ignored issues
show
Bug introduced by
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...
94
    }
95
96 7
    protected function filtrateByAttribute(&$mails, $attribute) {
97 7
        if (empty($this->$attribute)) {
98 7
            return;
99
        }
100
101 6
        $subjectTemplate = $this->$attribute;
102 6
        foreach ($mails as $mailKey => $mail) {
103 6
            $subject = $mail->$attribute;
104 6
            $isMatch = $this->isTemplateMatched($subject, $subjectTemplate);
105 6
            if (!$isMatch) {
106 6
                unset($mails[$mailKey]);
107
            }
108
        }
109 6
    }
110
111
    /**
112
     * @param $subject
113
     * @param $subjectTemplate
114
     * @return bool
115
     */
116 7
    protected function isTemplateMatched($subject, $subjectTemplate)
117
    {
118 7
        $isMatch = true;
119 7
        $subject = mb_strtolower($subject);
120 7
        $subject = trim($subject);
121 7
        $subjectTemplate = mb_strtolower($subjectTemplate);
122 7
        if (strpos($subjectTemplate, '*') !== false) {
123 4
            $parts = explode('*', $subjectTemplate);
124 4
            $prevMatchPos = false;
125 4
            foreach ($parts as $key => $part) {
126 4
                if (empty($part) && $key !== 0) {
127 2
                    if ($key === (count($parts) - 1)) {
128 2
                        break;
129
                    }
130
131
                    $matchPos = 0;
132
                } else {
133 4
                    if (empty($part) && $key === 0) {
134 1
                        $matchPos = 0;
135
                    } else {
136 4
                        $matchPos = strpos($subject, $part);
137
                    }
138
139 4
                    if ($key === 0 && $matchPos !== 0) {
140 2
                        $isMatch = false;
141 2
                        break;
142
                    }
143
                }
144
145 4
                if ($matchPos === false || ($prevMatchPos !== false && $matchPos < $prevMatchPos)) {
146
                    $isMatch = false;
147
                    break;
148
                }
149
150 4
                if ($key === (count($parts) - 1) && (strlen($subject) > (strlen($part) + $matchPos))) {
151 1
                    $isMatch = false;
152 1
                    break;
153
                }
154
155 4
                $prevMatchPos = $matchPos;
156
            }
157 4
        } else if ($subject !== $subjectTemplate) {
158 2
            $isMatch = false;
159
        }
160
161 7
        return $isMatch;
162
    }
163
}