Completed
Pull Request — master (#42)
by Frederik
04:41
created

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