HtmlPart::withoutHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Mime;
5
6
use Genkgo\Mail\Header\ContentTransferEncoding;
7
use Genkgo\Mail\Header\ContentType;
8
use Genkgo\Mail\HeaderInterface;
9
use Genkgo\Mail\Stream\OptimalTransferEncodedTextStream;
10
use Genkgo\Mail\StreamInterface;
11
12
final class HtmlPart implements PartInterface
13
{
14
    /**
15
     * @var PartInterface
16
     */
17
    private $decoratedPart;
18
19
    /**
20
     * @param string $html
21
     * @param string $charset
22
     */
23 22
    public function __construct(string $html, string $charset = 'UTF-8')
24
    {
25 22
        if ($html === '') {
26 1
            throw new \InvalidArgumentException('Received empty string instead of HTML');
27
        }
28
29 21
        $stream = new OptimalTransferEncodedTextStream($html);
30 21
        $encoding = $stream->getMetadata(['transfer-encoding'])['transfer-encoding'];
31
32 21
        $this->decoratedPart = (new GenericPart())
33 21
            ->withHeader(new ContentType('text/html', $charset))
34 21
            ->withHeader(new ContentTransferEncoding($encoding))
35 21
            ->withBody($stream);
36 21
    }
37
38
    /**
39
     * @return iterable<HeaderInterface>
0 ignored issues
show
Documentation introduced by
The doc-type iterable<HeaderInterface> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
40
     */
41 17
    public function getHeaders(): iterable
42
    {
43 17
        return $this->decoratedPart->getHeaders();
44
    }
45
46
    /**
47
     * @param string $name
48
     * @return bool
49
     */
50 1
    public function hasHeader(string $name): bool
51
    {
52 1
        return $this->decoratedPart->hasHeader($name);
53
    }
54
55
    /**
56
     * @param string $name
57
     * @return HeaderInterface
58
     */
59 18
    public function getHeader(string $name): HeaderInterface
60
    {
61 18
        return $this->decoratedPart->getHeader($name);
62
    }
63
64
    /**
65
     * @param HeaderInterface $header
66
     * @return PartInterface
67
     */
68 1
    public function withHeader(HeaderInterface $header): PartInterface
69
    {
70 1
        $clone = clone $this;
71 1
        $clone->decoratedPart = $this->decoratedPart->withHeader($header);
72 1
        return $clone;
73
    }
74
75
    /**
76
     * @param string $name
77
     * @return PartInterface
78
     */
79 1
    public function withoutHeader(string $name): PartInterface
80
    {
81 1
        $clone = clone $this;
82 1
        $clone->decoratedPart = $this->decoratedPart->withoutHeader($name);
83 1
        return $clone;
84
    }
85
86
    /**
87
     * @param StreamInterface $body
88
     * @return PartInterface
89
     */
90 1
    public function withBody(StreamInterface $body): PartInterface
91
    {
92 1
        throw new \RuntimeException('Cannot modify body of HtmlPart');
93
    }
94
95
    /**
96
     * @return StreamInterface
97
     */
98 17
    public function getBody(): StreamInterface
99
    {
100 17
        return $this->decoratedPart->getBody();
101
    }
102
}
103