Reply::withLine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Smtp;
5
6
use Genkgo\Mail\Exception\AssertionFailedException;
7
8
final class Reply
9
{
10
    /**
11
     * @var array<int, array<int|string>>
12
     */
13
    private $lines = [];
14
15
    /**
16
     * @var array<int, bool>
17
     */
18
    private $codes = [];
19
20
    /**
21
     * @var array<int, string>
22
     */
23
    private $messages = [];
24
25
    /**
26
     * @var Client
27
     */
28
    private $client;
29
30
    /**
31
     * @param Client $client
32
     */
33 32
    public function __construct(Client $client)
34
    {
35 32
        $this->client = $client;
36 32
    }
37
38
    /**
39
     * @return bool
40
     */
41 6
    public function isError(): bool
42
    {
43
        try {
44 6
            $this->assertBetween(400, 599);
45 2
            return true;
46 4
        } catch (AssertionFailedException $e) {
47 4
            return false;
48
        }
49
    }
50
51
    /**
52
     * @return bool
53
     */
54 14
    public function isCommandNotImplemented(): bool
55
    {
56
        try {
57 14
            $this->assert(502);
58 6
            return true;
59 8
        } catch (AssertionFailedException $e) {
60 8
            return false;
61
        }
62
    }
63
64
    /**
65
     * @return array<int, string>
0 ignored issues
show
Documentation introduced by
The doc-type array<int, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
66
     */
67 14
    public function getMessages(): array
68
    {
69 14
        return $this->messages;
70
    }
71
72
    /**
73
     * @param int $code
74
     * @param string $message
75
     * @return Reply
76
     */
77 32
    public function withLine(int $code, string $message): Reply
78
    {
79 32
        $clone = clone $this;
80 32
        $clone->lines[] = [$code, $message];
81 32
        $clone->codes[$code] = true;
82 32
        $clone->messages[] = $message;
83 32
        return $clone;
84
    }
85
86
    /**
87
     * @param int $code
88
     * @return Client
89
     */
90 15
    public function assert(int $code): Client
91
    {
92 15
        return $this->assertBetween($code, $code);
93
    }
94
95
    /**
96
     * @return Client
97
     */
98 21
    public function assertCompleted(): Client
99
    {
100 21
        return $this->assertBetween(200, 299);
101
    }
102
103
    /**
104
     * @return Client
105
     */
106 6
    public function assertIntermediate(): Client
107
    {
108 6
        return $this->assertBetween(300, 399);
109
    }
110
111
    /**
112
     * @param int $min
113
     * @param int $max
114
     * @return Client
115
     * @throws AssertionFailedException
116
     */
117 28
    private function assertBetween(int $min, int $max): Client
118
    {
119 28
        foreach ($this->codes as $code => $activated) {
120 28
            if ($code >= $min && $code <= $max) {
121 25
                return $this->client;
122
            }
123
        }
124
125 15
        throw new AssertionFailedException(
126 15
            \sprintf(
127 15
                'Cannot assert reply code between %s and %s. Server replied %s.',
128 15
                $min,
129 15
                $max,
130 15
                $this->createErrorMessage()
131
            )
132
        );
133
    }
134
135
    /**
136
     * @return string
137
     */
138 15
    private function createErrorMessage(): string
139
    {
140 15
        return \implode(
141 15
            "\r\n",
142 15
            \array_map(
143
                function ($values) {
144 15
                    return \implode(' ', $values);
145 15
                },
146 15
                $this->lines
147
            )
148
        );
149
    }
150
}
151