1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Smtp; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\EmailAddress; |
7
|
|
|
use Genkgo\Mail\MessageInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Session |
11
|
|
|
* @package Genkgo\Mail\Protocol\Smtp |
12
|
|
|
*/ |
13
|
|
|
final class Session |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
public CONST STATE_CONNECTED = 0; |
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
public CONST STATE_EHLO = 1; |
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
public CONST STATE_NEGOTIATION = 2; |
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
public CONST STATE_AUTHENTICATED = 3; |
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
public CONST STATE_MESSAGE = 4; |
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
*/ |
39
|
|
|
public CONST STATE_MESSAGE_RECEIVED = 5; |
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
public CONST STATE_DISCONNECT = 6; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
private $state = self::STATE_CONNECTED; |
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $command; |
53
|
|
|
/** |
54
|
|
|
* @var MessageInterface |
55
|
|
|
*/ |
56
|
|
|
private $message; |
57
|
|
|
/** |
58
|
|
|
* @var EmailAddress |
59
|
|
|
*/ |
60
|
|
|
private $envelope; |
61
|
|
|
/** |
62
|
|
|
* @var EmailAddress[] |
63
|
|
|
*/ |
64
|
|
|
private $recipients = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param int $state |
68
|
|
|
* @return Session |
69
|
|
|
*/ |
70
|
8 |
|
public function withState(int $state): self |
71
|
|
|
{ |
72
|
8 |
|
$clone = clone $this; |
73
|
8 |
|
$clone->state = $state; |
74
|
8 |
|
return $clone; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $command |
79
|
|
|
* @return Session |
80
|
|
|
*/ |
81
|
10 |
|
public function withCommand(string $command): self |
82
|
|
|
{ |
83
|
10 |
|
$clone = clone $this; |
84
|
10 |
|
$clone->command = $command; |
85
|
10 |
|
return $clone; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param EmailAddress $envelope |
90
|
|
|
* @return Session |
91
|
|
|
*/ |
92
|
3 |
|
public function withEnvelope(EmailAddress $envelope): self |
93
|
|
|
{ |
94
|
3 |
|
$clone = clone $this; |
95
|
3 |
|
$clone->state = self::STATE_MESSAGE; |
96
|
3 |
|
$clone->envelope = $envelope; |
97
|
3 |
|
return $clone; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param EmailAddress $recipient |
102
|
|
|
* @return Session |
103
|
|
|
*/ |
104
|
4 |
|
public function withRecipient(EmailAddress $recipient): self |
105
|
|
|
{ |
106
|
4 |
|
$clone = clone $this; |
107
|
4 |
|
$clone->state = self::STATE_MESSAGE; |
108
|
4 |
|
$clone->recipients[] = $recipient; |
109
|
4 |
|
return $clone; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param MessageInterface $message |
114
|
|
|
* @return Session |
115
|
|
|
*/ |
116
|
4 |
|
public function withMessage(MessageInterface $message): self |
117
|
|
|
{ |
118
|
4 |
|
$clone = clone $this; |
119
|
4 |
|
$clone->state = self::STATE_MESSAGE_RECEIVED; |
120
|
4 |
|
$clone->message = $message; |
121
|
4 |
|
return $clone; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
10 |
|
public function getCommand(): string |
128
|
|
|
{ |
129
|
10 |
|
if ($this->command === null) { |
130
|
1 |
|
throw new \UnexpectedValueException('No command'); |
131
|
|
|
} |
132
|
|
|
|
133
|
9 |
|
return $this->command; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
19 |
|
public function getState(): int |
140
|
|
|
{ |
141
|
19 |
|
return $this->state; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array|EmailAddress[] |
146
|
|
|
*/ |
147
|
3 |
|
public function getRecipients(): array |
148
|
|
|
{ |
149
|
3 |
|
return $this->recipients; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return MessageInterface |
154
|
|
|
*/ |
155
|
2 |
|
public function getMessage(): MessageInterface |
156
|
|
|
{ |
157
|
2 |
|
if ($this->message === null) { |
158
|
1 |
|
throw new \UnexpectedValueException('No message'); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $this->message; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return EmailAddress |
166
|
|
|
*/ |
167
|
2 |
|
public function getEnvelope(): EmailAddress |
168
|
|
|
{ |
169
|
2 |
|
if ($this->envelope === null) { |
170
|
1 |
|
throw new \UnexpectedValueException('No message'); |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
return $this->envelope; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|