PlainTextPart::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 PlainTextPart implements PartInterface
13
{
14
    /**
15
     * @var PartInterface
16
     */
17
    private $decoratedPart;
18
19
    /**
20
     * @param string $text
21
     * @param string $charset
22
     */
23 27
    public function __construct(string $text, string $charset = '')
24
    {
25 27
        $stream = new OptimalTransferEncodedTextStream($text);
26 27
        $encoding = $stream->getMetadata(['transfer-encoding'])['transfer-encoding'];
27
28 27
        $this->decoratedPart = (new GenericPart())
29 27
            ->withBody($stream)
30 27
            ->withHeader(new ContentType('text/plain', $charset))
31 27
            ->withHeader(new ContentTransferEncoding($encoding));
32 27
    }
33
34
    /**
35
     * @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...
36
     */
37 24
    public function getHeaders(): iterable
38
    {
39 24
        return $this->decoratedPart->getHeaders();
40
    }
41
42
    /**
43
     * @param string $name
44
     * @return bool
45
     */
46 1
    public function hasHeader(string $name): bool
47
    {
48 1
        return $this->decoratedPart->hasHeader($name);
49
    }
50
51
    /**
52
     * @param string $name
53
     * @return HeaderInterface
54
     */
55 24
    public function getHeader(string $name): HeaderInterface
56
    {
57 24
        return $this->decoratedPart->getHeader($name);
58
    }
59
60
    /**
61
     * @param HeaderInterface $header
62
     * @return PartInterface
63
     */
64 1
    public function withHeader(HeaderInterface $header): PartInterface
65
    {
66 1
        $clone = clone $this;
67 1
        $clone->decoratedPart = $this->decoratedPart->withHeader($header);
68 1
        return $clone;
69
    }
70
71
    /**
72
     * @param string $name
73
     * @return PartInterface
74
     */
75 1
    public function withoutHeader(string $name): PartInterface
76
    {
77 1
        $clone = clone $this;
78 1
        $clone->decoratedPart = $this->decoratedPart->withoutHeader($name);
79 1
        return $clone;
80
    }
81
82
    /**
83
     * @param StreamInterface $body
84
     * @return PartInterface
85
     */
86 1
    public function withBody(StreamInterface $body): PartInterface
87
    {
88 1
        throw new \RuntimeException('Cannot modify body of PlainTextPart');
89
    }
90
91
    /**
92
     * @return StreamInterface
93
     */
94 24
    public function getBody(): StreamInterface
95
    {
96 24
        return $this->decoratedPart->getBody();
97
    }
98
}
99