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

UntaggedResponse   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 34.62%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 90
ccs 9
cts 26
cp 0.3462
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
A withBody() 0 6 1
A assertCompletion() 0 19 3
A assertContinuation() 0 4 1
A assertTagged() 0 4 1
A getBody() 0 4 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
}