Passed
Pull Request — master (#171)
by Zaahid
07:22 queued 03:34
created

PartFilter   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 21
eloc 25
c 3
b 0
f 0
dl 0
loc 102
ccs 28
cts 28
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromContentType() 0 4 1
A fromInlineContentType() 0 6 3
B fromDisposition() 0 8 7
A fromHeaderValue() 0 10 4
A fromAttachmentFilter() 0 10 6
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Message;
8
9
use ZBateson\MailMimeParser\Message\IMessagePart;
10
use ZBateson\MailMimeParser\Message\IMimePart;
11
12
/**
13
 * Collection of static methods that return callables for common IMultiPart
14
 * child filters.
15
 *
16
 * @author Zaahid Bateson
17
 */
18
abstract class PartFilter
19
{
20
    /**
21
     * Provides an 'attachment' filter used by Message::getAttachmentPart.
22
     *
23
     * The method filters out the following types of parts:
24
     *  - text/plain and text/html parts that do not have an 'attachment'
25
     *    disposition
26
     *  - any part that returns true for isMultiPart()
27
     *  - any part that returns true for isSignaturePart()
28
     *
29
     * @return callable
30
     */
31 58
    public static function fromAttachmentFilter()
32
    {
33
        return function (IMessagePart $part) {
34 58
            $type = $part->getContentType();
35 58
            $disp = $part->getContentDisposition();
36 58
            if (in_array($type, [ 'text/plain', 'text/html' ]) && $disp !== null && strcasecmp($disp, 'inline') === 0) {
37 51
                return false;
38
            }
39 58
            return !(($part instanceof IMimePart)
40 58
                && ($part->isMultiPart() || $part->isSignaturePart()));
41 58
        };
42
    }
43
44
    /**
45
     * Provides a filter that keeps parts that contain a header of $name with a
46
     * value that matches $value (case insensitive).
47
     *
48
     * By default signed parts are excluded. Pass FALSE to the third parameter
49
     * to include them.
50
     *
51
     * @param string $name The header name to look up
52
     * @param string $value The value to match
53
     * @param bool $excludeSignedParts Optional signed parts exclusion (defaults
54
     *        to true).
55
     * @return callable
56
     */
57 3
    public static function fromHeaderValue($name, $value, $excludeSignedParts = true)
58
    {
59
        return function (IMessagePart $part) use ($name, $value, $excludeSignedParts) {
60 3
            if ($part instanceof IMimePart) {
61 2
                if ($excludeSignedParts && $part->isSignaturePart()) {
62 1
                    return false;
63
                }
64 1
                return (strcasecmp($part->getHeaderValue($name, ''), $value) === 0);
0 ignored issues
show
Bug introduced by
It seems like $part->getHeaderValue($name, '') can also be of type null; however, parameter $string1 of strcasecmp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                return (strcasecmp(/** @scrutinizer ignore-type */ $part->getHeaderValue($name, ''), $value) === 0);
Loading history...
65
            }
66 1
            return false;
67 3
        };
68
    }
69
70
    /**
71
     * Includes only parts that match the passed $mimeType in the return value
72
     * of a call to 'getContentType()'.
73
     * 
74
     * @param string $mimeType Mime type of parts to find.
75
     * @return callable
76
     */
77 10
    public static function fromContentType($mimeType)
78
    {
79
        return function (IMessagePart $part) use ($mimeType) {
80 10
            return strcasecmp($part->getContentType(), $mimeType) === 0;
81 10
        };
82
    }
83
84
    /**
85
     * Returns parts matching $mimeType that do not have a Content-Disposition
86
     * set to 'attachment'.
87
     *
88
     * @param string $mimeType Mime type of parts to find.
89
     * @return callable
90
     */
91 82
    public static function fromInlineContentType($mimeType)
92
    {
93
        return function (IMessagePart $part) use ($mimeType) {
94 82
            $disp = $part->getContentDisposition();
95 82
            return (strcasecmp($part->getContentType(), $mimeType) === 0) && ($disp === null
96 82
                || strcasecmp($disp, 'attachment') !== 0);
97 82
        };
98
    }
99
100
    /**
101
     * Finds parts with the passed disposition (matching against
102
     * IMessagePart::getContentDisposition()), optionally including
103
     * multipart parts and signed parts.
104
     * 
105
     * @param string $disposition The disposition to find.
106
     * @param bool $includeMultipart Optionally include multipart parts by
107
     *        passing true (defaults to false).
108
     * @param bool $includeSignedParts Optionally include signed parts (defaults
109
     *        to false).
110
     * @return callable
111
     */
112 7
    public static function fromDisposition($disposition, $includeMultipart = false, $includeSignedParts = false)
113
    {
114
        return function (IMessagePart $part) use ($disposition, $includeMultipart, $includeSignedParts) {
115 7
            if (($part instanceof IMimePart) && ((!$includeMultipart && $part->isMultiPart()) || (!$includeSignedParts && $part->isSignaturePart()))) {
116 3
                return false;
117
            }
118 7
            $disp = $part->getContentDisposition();
119 7
            return ($disp !== null && strcasecmp($disp, $disposition) === 0);
120 7
        };
121
    }
122
}
123