Test Failed
Push — 1.0.0 ( dd1332...30b11b )
by Zaahid
02:32
created

ParentHeaderPart::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 7
dl 0
loc 20
rs 9.9332
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
    public function __construct(
53
        PartStreamFilterManager $partStreamFilterManager,
54
        StreamFactory $streamFactory,
55
        PartFilterFactory $partFilterFactory,
56
        HeaderFactory $headerFactory,
57
        PartBuilder $partBuilder,
58
        StreamInterface $stream,
59
        StreamInterface $contentStream = null
60
    ) {
61
        parent::__construct(
62
            $partStreamFilterManager,
63
            $streamFactory,
64
            $partFilterFactory,
65
            $partBuilder,
66
            $stream,
67
            $contentStream
68
        );
69
        $this->headerFactory = $headerFactory;
70
        $this->headers['contenttype'] = $partBuilder->getContentType();
71
        $this->rawHeaders = $partBuilder->getRawHeaders();
72
    }
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
    private function getNormalizedHeaderName($header)
82
    {
83
        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
    public function getHeader($name)
95
    {
96
        $nameKey = $this->getNormalizedHeaderName($name);
97
        if (isset($this->rawHeaders[$nameKey])) {
98
            if (!isset($this->headers[$nameKey])) {
99
                $this->headers[$nameKey] = $this->headerFactory->newInstance(
100
                    $this->rawHeaders[$nameKey][0],
101
                    $this->rawHeaders[$nameKey][1]
102
                );
103
            }
104
            return $this->headers[$nameKey];
105
        }
106
        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
    public function getRawHeaders()
116
    {
117
        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
    public function getHeaderValue($name, $defaultValue = null)
130
    {
131
        $header = $this->getHeader($name);
132
        if ($header !== null) {
133
            return $header->getValue();
134
        }
135
        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
    public function getHeaderParameter($header, $param, $defaultValue = null)
153
    {
154
        $obj = $this->getHeader($header);
155
        if ($obj && $obj instanceof ParameterHeader) {
156
            return $obj->getValueFor($param, $defaultValue);
157
        }
158
        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
    public function setRawHeader($name, $value)
171
    {
172
        $normalized = $this->getNormalizedHeaderName($name);
173
        $header = $this->headerFactory->newInstance($name, $value);
174
        $this->headers[$normalized] = $header;
175
        $this->rawHeaders[$normalized] = [
176
            $header->getName(),
177
            $header->getRawValue()
178
        ];
179
        $this->onChange();
180
    }
181
182
    /**
183
     * Removes the header with the given name
184
     *
185
     * @param string $name
186
     */
187
    public function removeHeader($name)
188
    {
189
        $normalized = $this->getNormalizedHeaderName($name);
190
        unset($this->headers[$normalized], $this->rawHeaders[$normalized]);
191
        $this->onChange();
192
    }
193
}
194