|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Imap\Response; |
|
5
|
|
|
|
|
6
|
|
|
use Genkgo\Mail\Exception\AssertionFailedException; |
|
7
|
|
|
use Genkgo\Mail\Protocol\Imap\RequestInterface; |
|
8
|
|
|
use Genkgo\Mail\Protocol\Imap\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Response |
|
12
|
|
|
* @package Genkgo\Mail\Protocol\Smtp |
|
13
|
|
|
*/ |
|
14
|
|
|
final class AggregateResponse implements \IteratorAggregate |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array|ResponseInterface[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $lines = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var RequestInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $request; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Reply constructor. |
|
28
|
|
|
* @param RequestInterface $request |
|
29
|
|
|
*/ |
|
30
|
5 |
|
public function __construct(RequestInterface $request) |
|
31
|
|
|
{ |
|
32
|
5 |
|
$this->request = $request; |
|
33
|
5 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return \Iterator|ResponseInterface[] |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getIterator(): \Iterator |
|
39
|
|
|
{ |
|
40
|
|
|
return new \ArrayIterator($this->lines); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return ResponseInterface |
|
45
|
|
|
*/ |
|
46
|
5 |
|
public function first(): ResponseInterface |
|
47
|
|
|
{ |
|
48
|
5 |
|
if (empty($this->lines)) { |
|
49
|
|
|
throw new \UnexpectedValueException('Cannot return item of empty response'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
5 |
|
return reset($this->lines); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return ResponseInterface |
|
57
|
|
|
*/ |
|
58
|
5 |
|
public function last(): ResponseInterface |
|
59
|
|
|
{ |
|
60
|
5 |
|
if (empty($this->lines)) { |
|
61
|
|
|
throw new \UnexpectedValueException('Cannot return item of empty response'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
return end($this->lines); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param int $index |
|
69
|
|
|
* @return ResponseInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
public function at(int $index): ResponseInterface |
|
72
|
|
|
{ |
|
73
|
|
|
if (!isset($this->lines[$index])) { |
|
74
|
|
|
throw new \UnexpectedValueException('GenericItem not in response'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this->lines[$index]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
5 |
|
public function hasCompleted(): bool |
|
84
|
|
|
{ |
|
85
|
5 |
|
if (empty($this->lines)) { |
|
86
|
5 |
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
$lastCommand = end($this->lines); |
|
90
|
|
|
try { |
|
91
|
5 |
|
$lastCommand->assertTagged(); |
|
92
|
5 |
|
return true; |
|
93
|
5 |
|
} catch (AssertionFailedException $e) { |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
try { |
|
97
|
5 |
|
$lastCommand->assertContinuation(); |
|
98
|
2 |
|
return true; |
|
99
|
4 |
|
} catch (AssertionFailedException $e) { |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
4 |
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $line |
|
107
|
|
|
* @return AggregateResponse |
|
108
|
|
|
*/ |
|
109
|
5 |
|
public function withLine(string $line): AggregateResponse |
|
110
|
|
|
{ |
|
111
|
5 |
|
$clone = clone $this; |
|
112
|
|
|
|
|
113
|
5 |
|
if (!isset($line[0])) { |
|
114
|
|
|
throw new \InvalidArgumentException('Empty line'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
5 |
|
switch (substr($line, 0, 2)) { |
|
118
|
5 |
|
case '+ ': |
|
119
|
2 |
|
$clone->lines[] = new CommandContinuationRequestResponse( |
|
120
|
2 |
|
substr($line, 2) |
|
121
|
|
|
); |
|
122
|
2 |
|
break; |
|
123
|
5 |
|
case '* ': |
|
124
|
4 |
|
$clone->lines[] = new UntaggedResponse( |
|
125
|
4 |
|
substr($line, 2) |
|
126
|
|
|
); |
|
127
|
4 |
|
break; |
|
128
|
|
|
default: |
|
129
|
|
|
try { |
|
130
|
5 |
|
$tag = $this->request->getTag(); |
|
131
|
5 |
|
$clone->lines[] = new TaggedResponse($tag, $tag->extractBodyFromLine($line)); |
|
132
|
|
|
} catch (\InvalidArgumentException | \BadMethodCallException $e) { |
|
133
|
|
|
$keys = array_keys($clone->lines); |
|
134
|
|
|
$lastKey = end($keys); |
|
135
|
|
|
$clone->lines[$lastKey] = $clone->lines[$lastKey]->withBody($line); |
|
136
|
|
|
} |
|
137
|
5 |
|
break; |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
5 |
|
return $clone; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|