Completed
Pull Request — master (#42)
by Frederik
02:26
created

TaggedResponse::assertContinuation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
/**
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 22
    public function __construct(Tag $tag, string $line)
31
    {
32 22
        $this->tag = $tag;
33 22
        $this->line = $line;
34 22
    }
35
36
    /**
37
     * @return string
38
     */
39 1
    public function __toString(): string
40
    {
41 1
        return implode(' ', [(string)$this->tag, $this->line]);
42
    }
43
44
    /**
45
     * @param string $data
46
     * @return ResponseInterface
47
     */
48 2
    public function withAddedBody(string $data): ResponseInterface
49
    {
50 2
        $clone = clone $this;
51 2
        $clone->line .= $data;
52 2
        return $clone;
53
    }
54
55
    /**
56
     * @param CompletionResult $expectedResult
57
     * @return ResponseInterface
58
     * @throws AssertionFailedException
59
     */
60 9
    public function assertCompletion(CompletionResult $expectedResult): ResponseInterface
61
    {
62
        try {
63 9
            $completionResult = CompletionResult::fromLine($this->line);
64 8
            if (!$completionResult->equals($expectedResult)) {
65 1
                throw new AssertionFailedException(
66 1
                    sprintf(
67 1
                        'Returned completion, but %s is not equals to expected %s',
68 1
                        (string)$completionResult,
69 8
                        (string)$expectedResult
70
                    )
71
                );
72
            }
73 2
        } catch (\InvalidArgumentException $e) {
74 1
            throw new AssertionFailedException(
75 1
                sprintf(
76 1
                    'Response %s does not include a completion result',
77 1
                    $this->line
78
                )
79
            );
80
        }
81
82 7
        return $this;
83
    }
84
85
    /**
86
     * @return ResponseInterface
87
     * @throws AssertionFailedException
88
     */
89 1
    public function assertContinuation(): ResponseInterface
90
    {
91 1
        throw new AssertionFailedException();
92
    }
93
94
    /**
95
     * @return ResponseInterface
96
     */
97 10
    public function assertTagged(): ResponseInterface
98
    {
99 10
        return $this;
100
    }
101
102
    /**
103
     * @return string
104
     */
105 2
    public function getBody(): string
106
    {
107 2
        return $this->line;
108
    }
109
}