Passed
Push — master ( ad3faa...5c4918 )
by Zaahid
03:33
created

ParentHeaderPart::getNormalizedHeaderName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 Psr\Http\Message\StreamInterface;
10
use ZBateson\MailMimeParser\Header\HeaderFactory;
11
use ZBateson\MailMimeParser\Header\ParameterHeader;
12
use ZBateson\MailMimeParser\Stream\StreamFactory;
13
use ZBateson\MailMimeParser\Message\PartFilterFactory;
14
use ZBateson\MailMimeParser\Header\HeaderContainer;
15
16
/**
17
 * A parent part containing headers.
18
 *
19
 * @author Zaahid Bateson
20
 */
21
abstract class ParentHeaderPart extends ParentPart
22
{
23
    /**
24
     * @var HeaderContainer
25
     */
26
    protected $headerContainer;
27
28
    /**
29
     * @param PartStreamFilterManager $partStreamFilterManager
30
     * @param StreamFactory $streamFactory
31
     * @param PartFilterFactory $partFilterFactory
32
     * @param PartBuilder $partBuilder
33
     * @param StreamInterface $stream
34
     * @param StreamInterface $contentStream
35
     */
36 29
    public function __construct(
37
        PartStreamFilterManager $partStreamFilterManager,
38
        StreamFactory $streamFactory,
39
        PartFilterFactory $partFilterFactory,
40
        PartBuilder $partBuilder,
41
        StreamInterface $stream = null,
42
        StreamInterface $contentStream = null
43
    ) {
44 29
        parent::__construct(
45 29
            $partStreamFilterManager,
46 29
            $streamFactory,
47 29
            $partFilterFactory,
48 29
            $partBuilder,
49 29
            $stream,
50 29
            $contentStream
51
        );
52 29
        $this->headerContainer = $partBuilder->getHeaderContainer();
0 ignored issues
show
Documentation Bug introduced by
It seems like $partBuilder->getHeaderContainer() of type array is incompatible with the declared type ZBateson\MailMimeParser\Header\HeaderContainer of property $headerContainer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53 29
    }
54
55
    /**
56
     * Returns the AbstractHeader object for the header with the given $name
57
     *
58
     * Note that mime headers aren't case sensitive.
59
     *
60
     * @param string $name
61
     * @return \ZBateson\MailMimeParser\Header\AbstractHeader
62
     */
63 18
    public function getHeader($name, $offset = 0)
64
    {
65 18
        return $this->headerContainer->get($name, $offset);
66
    }
67
68
    /**
69
     * 
70
     * @param type $name
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\Message\Part\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
     */
72
    public function getAllHeadersByName($name)
73
    {
74
        return $this->headerContainer->getAll($name);
75
    }
76
77
    /**
78
     * Returns an array of all headers for the mime part with the first element
79
     * holding the name, and the second its value.
80
     *
81
     * @return string[][]
82
     */
83 1
    public function getRawHeaders()
84
    {
85 1
        return $this->headerContainer->getHeaders();
86
    }
87
88
    /**
89
     * 
90
     *
91
     * @return \Iterator
92
     */
93
    public function getRawHeaderIterator()
94
    {
95
        return $this->headerContainer->getIterator();
96
    }
97
98
    /**
99
     * Returns the string value for the header with the given $name.
100
     *
101
     * Note that mime headers aren't case sensitive.
102
     *
103
     * @param string $name
104
     * @param string $defaultValue
105
     * @return string
106
     */
107 14
    public function getHeaderValue($name, $defaultValue = null)
108
    {
109 14
        $header = $this->getHeader($name);
110 14
        if ($header !== null) {
111 13
            return $header->getValue();
112
        }
113 1
        return $defaultValue;
114
    }
115
116
    /**
117
     * Returns a parameter of the header $header, given the parameter named
118
     * $param.
119
     *
120
     * Only headers of type
121
     * \ZBateson\MailMimeParser\Header\ParameterHeader have parameters.
122
     * Content-Type and Content-Disposition are examples of headers with
123
     * parameters. "Charset" is a common parameter of Content-Type.
124
     *
125
     * @param string $header
126
     * @param string $param
127
     * @param string $defaultValue
128
     * @return string
129
     */
130 15
    public function getHeaderParameter($header, $param, $defaultValue = null)
131
    {
132 15
        $obj = $this->getHeader($header);
133 15
        if ($obj && $obj instanceof ParameterHeader) {
134 13
            return $obj->getValueFor($param, $defaultValue);
135
        }
136 2
        return $defaultValue;
137
    }
138
139
    /**
140
     * Adds a header with the given $name and $value.
141
     *
142
     * Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and
143
     * registers it as a header.
144
     *
145
     * @param string $name
146
     * @param string $value
147
     */
148 1
    public function setRawHeader($name, $value, $offset = 0)
149
    {
150 1
        $this->headerContainer->set($name, $value, $offset);
151 1
        $this->onChange();
152 1
    }
153
154
    /**
155
     * Adds a header with the given $name and $value.
156
     *
157
     * Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and
158
     * registers it as a header.
159
     *
160
     * @param string $name
161
     * @param string $value
162
     */
163 1
    public function addRawHeader($name, $value)
164
    {
165 1
        $this->headerContainer->add($name, $value);
166 1
        $this->onChange();
167 1
    }
168
169
    /**
170
     * Removes the header with the given name
171
     *
172
     * @param string $name
173
     */
174 1
    public function removeHeader($name)
175
    {
176 1
        $this->headerContainer->removeAll($name);
177 1
        $this->onChange();
178 1
    }
179
180
    /**
181
     * Removes the header with the given name
182
     *
183
     * @param string $name
184
     */
185 1
    public function removeSingleHeader($name, $offset = 0)
186
    {
187 1
        $this->headerContainer->remove($name, $offset);
188 1
        $this->onChange();
189 1
    }
190
}
191