|
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
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getCommand(): string |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->command; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getState(): int |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->state; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param int $state |
|
84
|
|
|
* @return Session |
|
85
|
|
|
*/ |
|
86
|
|
|
public function withState(int $state): self |
|
87
|
|
|
{ |
|
88
|
|
|
$clone = clone $this; |
|
89
|
|
|
$clone->state = $state; |
|
90
|
|
|
return $clone; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $command |
|
95
|
|
|
* @return Session |
|
96
|
|
|
*/ |
|
97
|
|
|
public function withCommand(string $command): self |
|
98
|
|
|
{ |
|
99
|
|
|
$clone = clone $this; |
|
100
|
|
|
$clone->command = $command; |
|
101
|
|
|
return $clone; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param EmailAddress $envelope |
|
106
|
|
|
* @return Session |
|
107
|
|
|
*/ |
|
108
|
|
|
public function withEnvelope(EmailAddress $envelope): self |
|
109
|
|
|
{ |
|
110
|
|
|
$clone = clone $this; |
|
111
|
|
|
$clone->state = self::STATE_MESSAGE; |
|
112
|
|
|
$clone->envelope = $envelope; |
|
113
|
|
|
return $clone; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param EmailAddress $recipient |
|
118
|
|
|
* @return Session |
|
119
|
|
|
*/ |
|
120
|
|
|
public function withRecipient(EmailAddress $recipient): self |
|
121
|
|
|
{ |
|
122
|
|
|
$clone = clone $this; |
|
123
|
|
|
$clone->state = self::STATE_MESSAGE; |
|
124
|
|
|
$clone->recipients[] = $recipient; |
|
125
|
|
|
return $clone; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param MessageInterface $message |
|
130
|
|
|
* @return Session |
|
131
|
|
|
*/ |
|
132
|
|
|
public function withMessage(MessageInterface $message): self |
|
133
|
|
|
{ |
|
134
|
|
|
$clone = clone $this; |
|
135
|
|
|
$clone->state = self::STATE_MESSAGE_RECEIVED; |
|
136
|
|
|
$clone->message = $message; |
|
137
|
|
|
return $clone; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return array|EmailAddress[] |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getRecipients(): array |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->recipients; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return MessageInterface |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getMessage(): MessageInterface |
|
152
|
|
|
{ |
|
153
|
|
|
if ($this->message === null) { |
|
154
|
|
|
throw new \UnexpectedValueException('No message'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $this->message; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return EmailAddress |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getEnvelope(): EmailAddress |
|
164
|
|
|
{ |
|
165
|
|
|
if ($this->envelope === null) { |
|
166
|
|
|
throw new \UnexpectedValueException('No message'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->envelope; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|