1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Smtp; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Exception\AssertionFailedException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Reply |
10
|
|
|
* @package Genkgo\Mail\Protocol\Smtp |
11
|
|
|
*/ |
12
|
|
|
final class Reply |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $lines = []; |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $codes = []; |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $messages = []; |
26
|
|
|
/** |
27
|
|
|
* @var Client |
28
|
|
|
*/ |
29
|
|
|
private $client; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Reply constructor. |
33
|
|
|
* @param Client $client |
34
|
|
|
*/ |
35
|
25 |
|
public function __construct(Client $client) |
36
|
|
|
{ |
37
|
25 |
|
$this->client = $client; |
38
|
25 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
6 |
|
public function isError(): bool |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
6 |
|
$this->assertBetween(400, 599); |
47
|
2 |
|
return true; |
48
|
4 |
|
} catch (AssertionFailedException $e) { |
49
|
4 |
|
return false; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
4 |
|
public function isCommandNotImplemented(): bool |
57
|
|
|
{ |
58
|
|
|
try { |
59
|
4 |
|
$this->assert(502); |
60
|
1 |
|
return true; |
61
|
3 |
|
} catch (AssertionFailedException $e) { |
62
|
3 |
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
13 |
|
public function getMessages(): array |
70
|
|
|
{ |
71
|
13 |
|
return $this->messages; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int $code |
76
|
|
|
* @param string $message |
77
|
|
|
* @return Reply |
78
|
|
|
*/ |
79
|
25 |
|
public function withLine(int $code, string $message): Reply |
80
|
|
|
{ |
81
|
25 |
|
$clone = clone $this; |
82
|
25 |
|
$clone->lines[] = [$code, $message]; |
83
|
25 |
|
$clone->codes[$code] = true; |
84
|
25 |
|
$clone->messages[] = $message; |
85
|
25 |
|
return $clone; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param int $code |
90
|
|
|
* @return Client |
91
|
|
|
*/ |
92
|
5 |
|
public function assert(int $code): Client { |
93
|
5 |
|
return $this->assertBetween($code, $code); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Client |
98
|
|
|
*/ |
99
|
13 |
|
public function assertCompleted(): Client { |
100
|
13 |
|
return $this->assertBetween(200, 299); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Client |
105
|
|
|
*/ |
106
|
6 |
|
public function assertIntermediate(): Client { |
107
|
6 |
|
return $this->assertBetween(300, 399); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param int $min |
112
|
|
|
* @param int $max |
113
|
|
|
* @return Client |
114
|
|
|
* @throws AssertionFailedException |
115
|
|
|
*/ |
116
|
20 |
|
private function assertBetween(int $min, int $max): Client |
117
|
|
|
{ |
118
|
20 |
|
foreach ($this->codes as $code => $activated) { |
119
|
20 |
|
if ($code >= $min && $code <= $max) { |
120
|
20 |
|
return $this->client; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
8 |
|
throw new AssertionFailedException( |
125
|
8 |
|
sprintf( |
126
|
8 |
|
'Cannot assert reply code between %s and %s. Server replied %s.', |
127
|
8 |
|
$min, |
128
|
8 |
|
$max, |
129
|
8 |
|
$this->createErrorMessage() |
130
|
|
|
) |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
8 |
|
private function createErrorMessage(): string |
138
|
|
|
{ |
139
|
8 |
|
return implode( |
140
|
8 |
|
"\r\n", |
141
|
8 |
|
array_map( |
142
|
8 |
|
function ($values) { |
143
|
8 |
|
return implode(' ', $values); |
144
|
8 |
|
}, |
145
|
8 |
|
$this->lines |
146
|
|
|
) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|