Completed
Pull Request — master (#42)
by Frederik
01:59
created

TaggedResponse::assertCompletion()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.3915

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 5
cts 14
cp 0.357
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 1
crap 5.3915
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 11
    public function __construct(Tag $tag, string $line)
31
    {
32 11
        $this->tag = $tag;
33 11
        $this->line = $line;
34 11
    }
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 5
    public function assertCompletion(CompletionResult $expectedResult): ResponseInterface
61
    {
62
        try {
63 5
            $completionResult = CompletionResult::fromLine($this->line);
64 5
            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 5
                        (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 5
        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 6
    public function assertTagged(): ResponseInterface
98
    {
99 6
        return $this;
100
    }
101
102
    /**
103
     * @return string
104
     */
105 1
    public function getBody(): string
106
    {
107 1
        return $this->line;
108
    }
109
}