Passed
Push — master ( 37aad3...325143 )
by Zaahid
03:26
created

ParentHeaderPart::removeHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
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
15
/**
16
 * A parent part containing headers.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
abstract class ParentHeaderPart extends ParentPart
21
{
22
    /**
23
     * @var HeaderFactory the HeaderFactory object used for created headers
24
     */
25
    protected $headerFactory;
26
27
    /**
28
     * @var string[][] array of headers, with keys set to lower-cased,
29
     *      alphanumeric characters of the header's name, and values set to an
30
     *      array of 2 elements, the first being the header's original name with
31
     *      non-alphanumeric characters and original case, and the second set to
32
     *      the header's value.
33
     */
34
    protected $rawHeaders;
35
36
    /**
37
     * @var AbstractHeader[] array of parsed header objects populated on-demand,
38
     * the key is set to the header's name lower-cased, and with
39
     * non-alphanumeric characters removed.
40
     */
41
    protected $headers;
42
43
    /**
44
     * @param PartStreamFilterManager $partStreamFilterManager
45
     * @param StreamFactory $streamFactory
46
     * @param PartFilterFactory $partFilterFactory
47
     * @param HeaderFactory $headerFactory
48
     * @param PartBuilder $partBuilder
49
     * @param StreamInterface $stream
50
     * @param StreamInterface $contentStream
51
     */
52 32
    public function __construct(
53
        PartStreamFilterManager $partStreamFilterManager,
54
        StreamFactory $streamFactory,
55
        PartFilterFactory $partFilterFactory,
56
        HeaderFactory $headerFactory,
57
        PartBuilder $partBuilder,
58
        StreamInterface $stream = null,
59
        StreamInterface $contentStream = null
60
    ) {
61 32
        parent::__construct(
62 32
            $partStreamFilterManager,
63 32
            $streamFactory,
64 32
            $partFilterFactory,
65 32
            $partBuilder,
66 32
            $stream,
67 32
            $contentStream
68
        );
69 32
        $this->headerFactory = $headerFactory;
70 32
        $this->headers['contenttype'] = $partBuilder->getContentType();
71 32
        $this->rawHeaders = $partBuilder->getRawHeaders();
72 32
    }
73
74
    /**
75
     * Returns the string in lower-case, and with non-alphanumeric characters
76
     * stripped out.
77
     *
78
     * @param string $header
79
     * @return string
80
     */
81 21
    private function getNormalizedHeaderName($header)
82
    {
83 21
        return preg_replace('/[^a-z0-9]/', '', strtolower($header));
84
    }
85
86
    /**
87
     * Returns the AbstractHeader object for the header with the given $name
88
     *
89
     * Note that mime headers aren't case sensitive.
90
     *
91
     * @param string $name
92
     * @return AbstractHeader
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\...age\Part\AbstractHeader 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...
93
     */
94 21
    public function getHeader($name)
95
    {
96 21
        $nameKey = $this->getNormalizedHeaderName($name);
97 21
        if (isset($this->rawHeaders[$nameKey])) {
98 19
            if (!isset($this->headers[$nameKey])) {
99 15
                $this->headers[$nameKey] = $this->headerFactory->newInstance(
100 15
                    $this->rawHeaders[$nameKey][0],
101 15
                    $this->rawHeaders[$nameKey][1]
102
                );
103
            }
104 19
            return $this->headers[$nameKey];
105
        }
106 3
        return null;
107
    }
108
109
    /**
110
     * Returns an array of all headers for the mime part with the first element
111
     * holding the name, and the second its value.
112
     *
113
     * @return string[][]
114
     */
115 1
    public function getRawHeaders()
116
    {
117 1
        return array_values($this->rawHeaders);
118
    }
119
120
    /**
121
     * Returns the string value for the header with the given $name.
122
     *
123
     * Note that mime headers aren't case sensitive.
124
     *
125
     * @param string $name
126
     * @param string $defaultValue
127
     * @return string
128
     */
129 16
    public function getHeaderValue($name, $defaultValue = null)
130
    {
131 16
        $header = $this->getHeader($name);
132 16
        if ($header !== null) {
133 15
            return $header->getValue();
134
        }
135 2
        return $defaultValue;
136
    }
137
138
    /**
139
     * Returns a parameter of the header $header, given the parameter named
140
     * $param.
141
     *
142
     * Only headers of type
143
     * \ZBateson\MailMimeParser\Header\ParameterHeader have parameters.
144
     * Content-Type and Content-Disposition are examples of headers with
145
     * parameters. "Charset" is a common parameter of Content-Type.
146
     *
147
     * @param string $header
148
     * @param string $param
149
     * @param string $defaultValue
150
     * @return string
151
     */
152 17
    public function getHeaderParameter($header, $param, $defaultValue = null)
153
    {
154 17
        $obj = $this->getHeader($header);
155 17
        if ($obj && $obj instanceof ParameterHeader) {
156 15
            return $obj->getValueFor($param, $defaultValue);
157
        }
158 2
        return $defaultValue;
159
    }
160
161
    /**
162
     * Adds a header with the given $name and $value.
163
     *
164
     * Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and
165
     * registers it as a header.
166
     *
167
     * @param string $name
168
     * @param string $value
169
     */
170 3
    public function setRawHeader($name, $value)
171
    {
172 3
        $normalized = $this->getNormalizedHeaderName($name);
173 3
        $header = $this->headerFactory->newInstance($name, $value);
174 3
        $this->headers[$normalized] = $header;
175 3
        $this->rawHeaders[$normalized] = [
176 3
            $header->getName(),
177 3
            $header->getRawValue()
178
        ];
179 3
        $this->onChange();
180 3
    }
181
182
    /**
183
     * Removes the header with the given name
184
     *
185
     * @param string $name
186
     */
187 1
    public function removeHeader($name)
188
    {
189 1
        $normalized = $this->getNormalizedHeaderName($name);
190 1
        unset($this->headers[$normalized], $this->rawHeaders[$normalized]);
191 1
        $this->onChange();
192 1
    }
193
}
194