Filter::isTemplateMatched()   C
last analyzed

Complexity

Conditions 16
Paths 12

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 16.283

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 46
ccs 26
cts 29
cp 0.8966
rs 5.5666
c 0
b 0
f 0
cc 16
nc 12
nop 2
crap 16.283

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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