Completed
Pull Request — master (#87)
by Frederik
03:37
created

TaggedResponse::withAddedBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
final class TaggedResponse implements ResponseInterface
11
{
12
    /**
13
     * @var Tag
14
     */
15
    private $tag;
16
17
    /**
18
     * @var string
19
     */
20
    private $line;
21
22
    /**
23
     * @param Tag $tag
24
     * @param string $line
25
     */
26 29
    public function __construct(Tag $tag, string $line)
27
    {
28 29
        $this->tag = $tag;
29 29
        $this->line = $line;
30 29
    }
31
32
    /**
33
     * @return string
34
     */
35 1
    public function __toString(): string
36
    {
37 1
        return \sprintf('%s %s', (string)$this->tag, $this->line);
38
    }
39
40
    /**
41
     * @param CompletionResult $expectedResult
42
     * @return ResponseInterface
43
     * @throws AssertionFailedException
44
     */
45 14
    public function assertCompletion(CompletionResult $expectedResult): ResponseInterface
46
    {
47
        try {
48 14
            $completionResult = CompletionResult::fromLine($this->line);
49 13
            if (!$completionResult->equals($expectedResult)) {
50 1
                throw new AssertionFailedException(
51 1
                    \sprintf(
52 1
                        'Returned completion, but %s is not equals to expected %s',
53 1
                        (string)$completionResult,
54 13
                        (string)$expectedResult
55
                    )
56
                );
57
            }
58 2
        } catch (\InvalidArgumentException $e) {
59 1
            throw new AssertionFailedException(
60 1
                \sprintf(
61 1
                    'Response %s does not include a completion result',
62 1
                    $this->line
63
                )
64
            );
65
        }
66
67 12
        return $this;
68
    }
69
70
    /**
71
     * @return ResponseInterface
72
     * @throws AssertionFailedException
73
     */
74 1
    public function assertContinuation(): ResponseInterface
75
    {
76 1
        throw new AssertionFailedException();
77
    }
78
79
    /**
80
     * @return ResponseInterface
81
     */
82 24
    public function assertTagged(): ResponseInterface
83
    {
84 24
        return $this;
85
    }
86
87
    /**
88
     * @param string $className
89
     * @return ResponseInterface
90
     * @throws AssertionFailedException
91
     */
92
    public function assertParsed(string $className): ResponseInterface
93
    {
94
        throw new AssertionFailedException('A tagged response is never parsed');
95
    }
96
97
    /**
98
     * @return string
99
     */
100 1
    public function getBody(): string
101
    {
102 1
        return $this->line;
103
    }
104
}
105