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\ResponseInterface; |
8
|
|
|
use Genkgo\Mail\Protocol\Imap\Tag; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class TaggedResponse |
12
|
|
|
* @package Genkgo\Mail\Protocol\Imap\Response |
13
|
|
|
*/ |
14
|
|
|
final class TaggedResponse implements ResponseInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Tag |
18
|
|
|
*/ |
19
|
|
|
private $tag; |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $line; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* TaggedResponse constructor. |
27
|
|
|
* @param Tag $tag |
28
|
|
|
* @param string $line |
29
|
|
|
*/ |
30
|
4 |
|
public function __construct(Tag $tag, string $line) |
31
|
|
|
{ |
32
|
4 |
|
$this->tag = $tag; |
33
|
4 |
|
$this->line = $line; |
34
|
4 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function __toString(): string |
40
|
|
|
{ |
41
|
|
|
return implode(' ', [(string)$this->tag, $this->line]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $data |
46
|
|
|
* @return ResponseInterface |
47
|
|
|
*/ |
48
|
|
|
public function withBody(string $data): ResponseInterface |
49
|
|
|
{ |
50
|
|
|
$clone = clone $this; |
51
|
|
|
$clone->line .= $data; |
52
|
|
|
return $clone; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param CompletionResult $expectedResult |
57
|
|
|
* @return ResponseInterface |
58
|
|
|
* @throws AssertionFailedException |
59
|
|
|
*/ |
60
|
4 |
|
public function assertCompletion(CompletionResult $expectedResult): ResponseInterface |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
4 |
|
$completionResult = CompletionResult::fromLine($this->line); |
64
|
4 |
|
if (!$completionResult->equals($expectedResult)) { |
65
|
|
|
throw new AssertionFailedException( |
66
|
|
|
sprintf( |
67
|
|
|
'Returned completion, but %s is not equals to expected %s', |
68
|
|
|
(string)$completionResult, |
69
|
4 |
|
(string)$expectedResult |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} catch (\InvalidArgumentException $e) { |
74
|
|
|
throw new AssertionFailedException( |
75
|
|
|
sprintf( |
76
|
|
|
'Response %s does not include a completion result', |
77
|
|
|
$this->line |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return ResponseInterface |
87
|
|
|
* @throws AssertionFailedException |
88
|
|
|
*/ |
89
|
|
|
public function assertContinuation(): ResponseInterface |
90
|
|
|
{ |
91
|
|
|
throw new AssertionFailedException(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return ResponseInterface |
96
|
|
|
*/ |
97
|
4 |
|
public function assertTagged(): ResponseInterface |
98
|
|
|
{ |
99
|
4 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getBody(): string |
106
|
|
|
{ |
107
|
|
|
return $this->line; |
108
|
|
|
} |
109
|
|
|
} |