Passed
Push — master ( a0eabd...66d82e )
by Zaahid
03:20
created

MimePart::getContentId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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\Part;
8
9
use ZBateson\MailMimeParser\Message\PartFilter;
10
11
/**
12
 * Represents a single part of a multi-part mime message.
13
 *
14
 * A MimePart object may have any number of child parts, or may be a child
15
 * itself with its own parent or parents.
16
 *
17
 * The content of the part can be read from its PartStream resource handle,
18
 * accessible via MessagePart::getContentResourceHandle.
19
 *
20
 * @author Zaahid Bateson
21
 */
22
class MimePart extends ParentHeaderPart
23
{
24
    /**
25
     * Returns true if this part's mime type is multipart/*
26
     *
27
     * @return bool
28
     */
29 7
    public function isMultiPart()
30
    {
31
        // casting to bool, preg_match returns 1 for true
32 7
        return (bool) (preg_match(
33 7
            '~multipart/\w+~i',
34 7
            $this->getContentType()
35
        ));
36
    }
37
    
38
    /**
39
     * Returns a filename for the part if one is defined, or null otherwise.
40
     * 
41
     * @return string
42
     */
43 1
    public function getFilename()
44
    {
45 1
        return $this->getHeaderParameter(
46 1
            'Content-Disposition',
47 1
            'filename',
48 1
            $this->getHeaderParameter(
49 1
                'Content-Type',
50 1
                'name'
51
            )
52
        );
53
    }
54
    
55
    /**
56
     * Returns true.
57
     * 
58
     * @return bool
59
     */
60 1
    public function isMime()
61
    {
62 1
        return true;
63
    }
64
    
65
    /**
66
     * Returns true if this part's mime type is text/plain, text/html or if the
67
     * Content-Type header defines a charset.
68
     * 
69
     * @return bool
70
     */
71 7
    public function isTextPart()
72
    {
73 7
        return ($this->getCharset() !== null);
74
    }
75
    
76
    /**
77
     * Returns the lower-cased, trimmed value of the Content-Type header.
78
     * 
79
     * Parses the Content-Type header, defaults to returning text/plain if not
80
     * defined.
81
     *
82
     * @param string $default pass to override the returned value when not set
83
     * @return string
84
     */
85 9
    public function getContentType($default = 'text/plain')
86
    {
87 9
        return trim(strtolower($this->getHeaderValue('Content-Type', $default)));
88
    }
89
    
90
    /**
91
     * Returns the upper-cased charset of the Content-Type header's charset
92
     * parameter if set, ISO-8859-1 if the Content-Type is text/plain or
93
     * text/html and the charset parameter isn't set, or null otherwise.
94
     * 
95
     * @return string
96
     */
97 11
    public function getCharset()
98
    {
99 11
        $charset = $this->getHeaderParameter('Content-Type', 'charset');
100 11
        if ($charset === null) {
0 ignored issues
show
introduced by
The condition $charset === null is always false.
Loading history...
101 8
            $contentType = $this->getContentType();
102 8
            if ($contentType === 'text/plain' || $contentType === 'text/html') {
103 3
                return 'ISO-8859-1';
104
            }
105 5
            return null;
106
        }
107 3
        return trim(strtoupper($charset));
108
    }
109
    
110
    /**
111
     * Returns the content's disposition, defaulting to 'inline' if not set.
112
     *
113
     * @param string $default pass to override the default returned disposition
114
     *        when not set.
115
     * @return string
116
     */
117 1
    public function getContentDisposition($default = 'inline')
118
    {
119 1
        return strtolower($this->getHeaderValue('Content-Disposition', $default));
120
    }
121
    
122
    /**
123
     * Returns the content-transfer-encoding used for this part, defaulting to
124
     * '7bit' if not set.
125
     *
126
     * @param string $default pass to override the default when not set.
127
     * @return string
128
     */
129 2
    public function getContentTransferEncoding($default = '7bit')
130
    {
131 2
        static $translated = [
132
            'x-uue' => 'x-uuencode',
133
            'uue' => 'x-uuencode',
134
            'uuencode' => 'x-uuencode'
135
        ];
136 2
        $type = strtolower($this->getHeaderValue('Content-Transfer-Encoding', $default));
137 2
        if (isset($translated[$type])) {
138 1
            return $translated[$type];
139
        }
140 1
        return $type;
141
    }
142
143
    /**
144
     * Returns the Content ID of the part.
145
     *
146
     * Parses the value of the Content-ID header, stripping out the surrounding
147
     * '<' and '>' characters, and returning just the ID if set... or null if
148
     * not set or incorrectly formatted.
149
     *
150
     * @return string|null
151
     */
152 1
    public function getContentId()
153
    {
154 1
        $header = $this->getHeader('Content-ID');
155 1
        if ($header !== null) {
156 1
            return $header->getId();
0 ignored issues
show
Bug introduced by
The method getId() does not exist on ZBateson\MailMimeParser\Header\AbstractHeader. It seems like you code against a sub-type of ZBateson\MailMimeParser\Header\AbstractHeader such as ZBateson\MailMimeParser\Header\IdHeader. ( Ignorable by Annotation )

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

156
            return $header->/** @scrutinizer ignore-call */ getId();
Loading history...
157
        }
158 1
    }
159
160
    /**
161
     * Convenience method to find a part by its Content-ID header.
162
     *
163
     * @param string $contentId
164
     * @return MessagePart
165
     */
166 1
    public function getPartByContentId($contentId)
167
    {
168 1
        $sanitized = preg_replace('/^\s*<|>\s*$/', '', $contentId);
169 1
        $filter = $this->partFilterFactory->newFilterFromArray([
170
            'headers' => [
171 1
                PartFilter::FILTER_INCLUDE => [
172 1
                    'Content-ID' => $sanitized
173
                ]
174
            ]
175
        ]);
176 1
        return $this->getPart(0, $filter);
177
    }
178
}
179