Completed
Push — 1.0.0 ( 608796...a0ab23 )
by Zaahid
04:48
created

MimePart   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 78.95%

Importance

Changes 0
Metric Value
dl 0
loc 145
ccs 30
cts 38
cp 0.7895
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilename() 0 8 1
A getContentType() 0 3 1
A isMultiPart() 0 6 1
A getCharset() 0 11 4
A getContentDisposition() 0 3 1
A isTextPart() 0 3 1
A getContentTransferEncoding() 0 12 2
A isMime() 0 3 1
A __construct() 0 11 1
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 Psr\Http\Message\StreamInterface;
10
use ZBateson\MailMimeParser\Header\HeaderFactory;
11
use ZBateson\MailMimeParser\Message\PartFilterFactory;
12
13
/**
14
 * Represents a single part of a multi-part mime message.
15
 *
16
 * A MimePart object may have any number of child parts, or may be a child
17
 * itself with its own parent or parents.
18
 *
19
 * The content of the part can be read from its PartStream resource handle,
20
 * accessible via MessagePart::getContentResourceHandle.
21
 *
22
 * @author Zaahid Bateson
23
 */
24
class MimePart extends MessagePart
25
{
26
    use MimePartHeaderTrait {
27
        MimePartHeaderTrait::__construct as private mimePartHeaderConstructor;
28
    }
29
    use MimePartChildrenTrait {
30
        MimePartChildrenTrait::__construct as private mimePartChildrenConstructor;
31
    }
32
33
    /**
34
     * Sets up class dependencies.
35
     *
36
     * @param HeaderFactory $headerFactory 
37
     * @param PartFilterFactory $partFilterFactory
38
     * @param PartBuilder $partBuilder
39
     * @param PartStreamFilterManager $partStreamFilterManager
40
     * @param StreamInterface $stream
41
     * @param StreamInterface $contentStream
42
     */
43 24
    public function __construct(
44
        HeaderFactory $headerFactory,
45
        PartFilterFactory $partFilterFactory,
46
        PartBuilder $partBuilder,
47
        PartStreamFilterManager $partStreamFilterManager,
48
        StreamInterface $stream,
49
        StreamInterface $contentStream = null
50
    ) {
51 24
        parent::__construct($partStreamFilterManager, $stream, $contentStream);
0 ignored issues
show
Bug introduced by
$partStreamFilterManager of type ZBateson\MailMimeParser\...PartStreamFilterManager is incompatible with the type ZBateson\MailMimeParser\Header\HeaderFactory expected by parameter $headerFactory of ZBateson\MailMimeParser\...derTrait::__construct(). ( Ignorable by Annotation )

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

51
        parent::__construct(/** @scrutinizer ignore-type */ $partStreamFilterManager, $stream, $contentStream);
Loading history...
Bug introduced by
$stream of type Psr\Http\Message\StreamInterface is incompatible with the type ZBateson\MailMimeParser\Message\Part\PartBuilder expected by parameter $partBuilder of ZBateson\MailMimeParser\...derTrait::__construct(). ( Ignorable by Annotation )

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

51
        parent::__construct($partStreamFilterManager, /** @scrutinizer ignore-type */ $stream, $contentStream);
Loading history...
Unused Code introduced by
The call to ZBateson\MailMimeParser\...derTrait::__construct() has too many arguments starting with $contentStream. ( Ignorable by Annotation )

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

51
        parent::/** @scrutinizer ignore-call */ 
52
                __construct($partStreamFilterManager, $stream, $contentStream);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
52 24
        $this->mimePartChildrenConstructor($partFilterFactory, $partBuilder, $stream);
53 24
        $this->mimePartHeaderConstructor($headerFactory, $partBuilder);
54 24
    }
55
56
    /**
57
     * Returns true if this part's mime type is multipart/*
58
     *
59
     * @return bool
60
     */
61 7
    public function isMultiPart()
62
    {
63
        // casting to bool, preg_match returns 1 for true
64 7
        return (bool) (preg_match(
65 7
            '~multipart/\w+~i',
66 7
            $this->getContentType()
67
        ));
68
    }
69
    
70
    /**
71
     * Returns a filename for the part if one is defined, or null otherwise.
72
     * 
73
     * @return string
74
     */
75
    public function getFilename()
76
    {
77
        return $this->getHeaderParameter(
78
            'Content-Disposition',
79
            'filename',
80
            $this->getHeaderParameter(
81
                'Content-Type',
82
                'name'
83
            )
84
        );
85
    }
86
    
87
    /**
88
     * Returns true.
89
     * 
90
     * @return bool
91
     */
92 1
    public function isMime()
93
    {
94 1
        return true;
95
    }
96
    
97
    /**
98
     * Returns true if this part's mime type is text/plain, text/html or if the
99
     * Content-Type header defines a charset.
100
     * 
101
     * @return bool
102
     */
103 7
    public function isTextPart()
104
    {
105 7
        return ($this->getCharset() !== null);
106
    }
107
    
108
    /**
109
     * Returns the lower-cased, trimmed value of the Content-Type header.
110
     * 
111
     * Parses the Content-Type header, defaults to returning text/plain if not
112
     * defined.
113
     * 
114
     * @return string
115
     */
116 10
    public function getContentType($default = 'text/plain')
117
    {
118 10
        return trim(strtolower($this->getHeaderValue('Content-Type', $default)));
119
    }
120
    
121
    /**
122
     * Returns the upper-cased charset of the Content-Type header's charset
123
     * parameter if set, US-ASCII if the Content-Type is text/plain or text/html
124
     * and the charset parameter isn't set, or null otherwise.
125
     * 
126
     * @return string
127
     */
128 12
    public function getCharset()
129
    {
130 12
        $charset = $this->getHeaderParameter('Content-Type', 'charset');
131 12
        if ($charset === null) {
0 ignored issues
show
introduced by
The condition $charset === null is always false.
Loading history...
132 8
            $contentType = $this->getContentType();
133 8
            if ($contentType === 'text/plain' || $contentType === 'text/html') {
134 3
                return 'US-ASCII';
135
            }
136 5
            return null;
137
        }
138 4
        return trim(strtoupper($charset));
139
    }
140
    
141
    /**
142
     * Returns the content's disposition, defaulting to 'inline' if not set.
143
     * 
144
     * @return string
145
     */
146 1
    public function getContentDisposition($default = 'inline')
147
    {
148 1
        return strtolower($this->getHeaderValue('Content-Disposition', $default));
149
    }
150
    
151
    /**
152
     * Returns the content-transfer-encoding used for this part, defaulting to
153
     * '7bit' if not set.
154
     * 
155
     * @return string
156
     */
157 2
    public function getContentTransferEncoding($default = '7bit')
158
    {
159 2
        static $translated = [
160
            'x-uue' => 'x-uuencode',
161
            'uue' => 'x-uuencode',
162
            'uuencode' => 'x-uuencode'
163
        ];
164 2
        $type = strtolower($this->getHeaderValue('Content-Transfer-Encoding', $default));
165 2
        if (isset($translated[$type])) {
166
            return $translated[$type];
167
        }
168 2
        return $type;
169
    }
170
}
171