Completed
Pull Request — master (#50)
by Frederik
09:33
created

ReplyMessage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 9
dl 0
loc 125
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 33 5
A getHeaders() 0 4 1
A hasHeader() 0 4 1
A getHeader() 0 4 1
A withHeader() 0 4 1
A withAddedHeader() 0 4 1
A withoutHeader() 0 4 1
A getBody() 0 4 1
A withBody() 0 4 1
A __toString() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail;
5
6
use Genkgo\Mail\Header\From;
7
use Genkgo\Mail\Header\HeaderName;
8
use Genkgo\Mail\Header\ParsedHeader;
9
use Genkgo\Mail\Header\To;
10
11
final class ReplyMessage implements MessageInterface
12
{
13
    /**
14
     * @var MessageInterface
15
     */
16
    private $decoratedMessage;
17
18
    /**
19
     * @param MessageInterface $originalMessage
20
     * @param MessageBodyCollection $body
21
     * @param QuoteInterface $quotationMethod
22
     * @param Address $fromAddress
23
     */
24
    public function __construct(
25
        MessageInterface $originalMessage,
26
        MessageBodyCollection $body,
27
        QuoteInterface $quotationMethod,
28
        Address $fromAddress
29
    )
30
    {
31
        $this->decoratedMessage = (new GenericMessage())
32
            ->withHeader(new From($fromAddress));
33
34
        foreach ($originalMessage->getHeader('Message-ID') as $header) {
35
            $this->decoratedMessage = $this->decoratedMessage->withHeader(
36
                new ParsedHeader(
37
                    new HeaderName('In-Reply-To'),
38
                    $header->getValue()
39
                )
40
            );
41
            break;
42
        }
43
44
        $to = $originalMessage->hasHeader('Reply-To') ? 'Reply-To' : 'To';
45
        if ($originalMessage->hasHeader($to)) {
46
            foreach ($originalMessage->getHeader($to) as $header) {
47
                $this->decoratedMessage = $this->decoratedMessage
48
                    ->withHeader(
49
                        new To(AddressList::fromString((string)$header->getValue()))
50
                    );
51
            }
52
        }
53
54
        $this->decoratedMessage = $quotationMethod->quote($body, $originalMessage)
55
            ->attachToMessage($this->decoratedMessage);
56
    }
57
58
    /**
59
     * @return iterable
60
     */
61
    public function getHeaders(): iterable
62
    {
63
        return $this->decoratedMessage->getHeaders();
64
    }
65
66
    /**
67
     * @param string $name
68
     * @return bool
69
     */
70
    public function hasHeader(string $name): bool
71
    {
72
        return $this->decoratedMessage->hasHeader($name);
73
    }
74
75
    /**
76
     * @param string $name
77
     * @return iterable|HeaderInterface[]
78
     */
79
    public function getHeader(string $name): iterable
80
    {
81
        return $this->decoratedMessage->getHeader($name);
82
    }
83
84
    /**
85
     * @param HeaderInterface $header
86
     * @return MessageInterface
87
     */
88
    public function withHeader(HeaderInterface $header): MessageInterface
89
    {
90
        return $this->decoratedMessage->withHeader($header);
91
    }
92
93
    /**
94
     * @param HeaderInterface $header
95
     * @return MessageInterface
96
     */
97
    public function withAddedHeader(HeaderInterface $header): MessageInterface
98
    {
99
        return $this->decoratedMessage->withAddedHeader($header);
100
    }
101
102
    /**
103
     * @param string $name
104
     * @return MessageInterface
105
     */
106
    public function withoutHeader(string $name): MessageInterface
107
    {
108
        return $this->decoratedMessage->withoutHeader($name);
109
    }
110
111
    /**
112
     * @return StreamInterface
113
     */
114
    public function getBody(): StreamInterface
115
    {
116
        return $this->decoratedMessage->getBody();
117
    }
118
119
    /**
120
     * @param StreamInterface $body
121
     * @return MessageInterface
122
     */
123
    public function withBody(StreamInterface $body): MessageInterface
124
    {
125
        return $this->decoratedMessage->withBody($body);
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function __toString(): string
132
    {
133
        return (string)$this->decoratedMessage;
134
    }
135
}
136