PlainTextMessage   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 112
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A getHeaders() 0 4 1
A hasHeader() 0 4 1
A getHeader() 0 4 1
A withHeader() 0 6 1
A withAddedHeader() 0 6 1
A withoutHeader() 0 6 1
A getBody() 0 4 1
A withBody() 0 6 1
A __toString() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail;
5
6
use Genkgo\Mail\Header\ContentTransferEncoding;
7
use Genkgo\Mail\Header\ContentType;
8
use Genkgo\Mail\Stream\OptimalTransferEncodedTextStream;
9
10
final class PlainTextMessage implements MessageInterface
11
{
12
    /**
13
     * @var MessageInterface
14
     */
15
    private $decoratedMessage;
16
17
    /**
18
     * @param string $text
19
     * @param string $charset
20
     */
21 22
    public function __construct(string $text, string $charset = 'UTF-8')
22
    {
23 22
        $stream = new OptimalTransferEncodedTextStream($text);
24 22
        $encoding = $stream->getMetadata(['transfer-encoding'])['transfer-encoding'];
25
26 22
        if ($encoding === '7bit') {
27 22
            $charset = 'us-ascii';
28
        }
29
30 22
        $this->decoratedMessage = (new GenericMessage())
31 22
            ->withHeader(new ContentType('text/plain', $charset))
32 22
            ->withBody($stream)
33 22
            ->withHeader(new ContentTransferEncoding($encoding));
34 22
    }
35
36
    /**
37
     * @return iterable<iterable<HeaderInterface>>
0 ignored issues
show
Documentation introduced by
The doc-type iterable<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...
38
     */
39 7
    public function getHeaders(): iterable
40
    {
41 7
        return $this->decoratedMessage->getHeaders();
42
    }
43
44
    /**
45
     * @param string $name
46
     * @return bool
47
     */
48 2
    public function hasHeader(string $name): bool
49
    {
50 2
        return $this->decoratedMessage->hasHeader($name);
51
    }
52
53
    /**
54
     * @param string $name
55
     * @return iterable|HeaderInterface[]
56
     */
57 2
    public function getHeader(string $name): iterable
58
    {
59 2
        return $this->decoratedMessage->getHeader($name);
60
    }
61
62
    /**
63
     * @param HeaderInterface $header
64
     * @return MessageInterface
65
     */
66 9
    public function withHeader(HeaderInterface $header): MessageInterface
67
    {
68 9
        $clone = clone $this;
69 9
        $clone->decoratedMessage = $clone->decoratedMessage->withHeader($header);
70 9
        return $clone;
71
    }
72
73
    /**
74
     * @param HeaderInterface $header
75
     * @return MessageInterface
76
     */
77 1
    public function withAddedHeader(HeaderInterface $header): MessageInterface
78
    {
79 1
        $clone = clone $this;
80 1
        $clone->decoratedMessage = $clone->decoratedMessage->withAddedHeader($header);
81 1
        return $clone;
82
    }
83
84
    /**
85
     * @param string $name
86
     * @return MessageInterface
87
     */
88 1
    public function withoutHeader(string $name): MessageInterface
89
    {
90 1
        $clone = clone $this;
91 1
        $clone->decoratedMessage = $clone->decoratedMessage->withoutHeader($name);
92 1
        return $clone;
93
    }
94
95
    /**
96
     * @return StreamInterface
97
     */
98 7
    public function getBody(): StreamInterface
99
    {
100 7
        return $this->decoratedMessage->getBody();
101
    }
102
103
    /**
104
     * @param StreamInterface $body
105
     * @return MessageInterface
106
     */
107 1
    public function withBody(StreamInterface $body): MessageInterface
108
    {
109 1
        $clone = clone $this;
110 1
        $clone->decoratedMessage = $clone->decoratedMessage->withBody($body);
111 1
        return $clone;
112
    }
113
114
    /**
115
     * @return string
116
     */
117 8
    public function __toString(): string
118
    {
119 8
        return $this->decoratedMessage->__toString();
120
    }
121
}
122