Completed
Pull Request — master (#32)
by Gawain
01:49
created

Reply::isCommandNotImplemented()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Smtp;
5
6
use Genkgo\Mail\Exception\AssertionFailedException;
7
8
/**
9
 * Class Reply
10
 * @package Genkgo\Mail\Protocol\Smtp
11
 */
12
final class Reply
13
{
14
    /**
15
     * @var array
16
     */
17
    private $lines = [];
18
    /**
19
     * @var array
20
     */
21
    private $codes = [];
22
    /**
23
     * @var array
24
     */
25
    private $messages = [];
26
    /**
27
     * @var Client
28
     */
29
    private $client;
30
31
    /**
32
     * Reply constructor.
33
     * @param Client $client
34
     */
35 28
    public function __construct(Client $client)
36
    {
37 28
        $this->client = $client;
38 28
    }
39
40
    /**
41
     * @return bool
42
     */
43 6
    public function isError(): bool
44
    {
45
        try {
46 6
            $this->assertBetween(400, 599);
47 2
            return true;
48 4
        } catch (AssertionFailedException $e) {
49 4
            return false;
50
        }
51
    }
52
53
    /**
54
     * @return bool
55
     */
56 5
    public function isCommandNotImplemented(): bool
57
    {
58
        try {
59 5
            $this->assert(502);
60 2
            return true;
61 3
        } catch (AssertionFailedException $e) {
62 3
            return false;
63
        }
64
    }
65
66
    /**
67
     * @return array
68
     */
69 16
    public function getMessages(): array
70
    {
71 16
        return $this->messages;
72
    }
73
74
    /**
75
     * @param int $code
76
     * @param string $message
77
     * @return Reply
78
     */
79 28
    public function withLine(int $code, string $message): Reply
80
    {
81 28
        $clone = clone $this;
82 28
        $clone->lines[] = [$code, $message];
83 28
        $clone->codes[$code] = true;
84 28
        $clone->messages[] = $message;
85 28
        return $clone;
86
    }
87
88
    /**
89
     * @param int $code
90
     * @return Client
91
     */
92 6
    public function assert(int $code): Client {
93 6
        return $this->assertBetween($code, $code);
94
    }
95
96
    /**
97
     * @return Client
98
     */
99 16
    public function assertCompleted(): Client {
100 16
        return $this->assertBetween(200, 299);
101
    }
102
103
    /**
104
     * @return Client
105
     */
106 6
    public function assertIntermediate(): Client {
107 6
        return $this->assertBetween(300, 399);
108
    }
109
110
    /**
111
     * @param int $min
112
     * @param int $max
113
     * @return Client
114
     * @throws AssertionFailedException
115
     */
116 23
    private function assertBetween(int $min, int $max): Client
117
    {
118 23
        foreach ($this->codes as $code => $activated) {
119 23
            if ($code >= $min && $code <= $max) {
120 23
                return $this->client;
121
            }
122
        }
123
124 8
        throw new AssertionFailedException(
125 8
            sprintf(
126 8
                'Cannot assert reply code between %s and %s. Server replied %s.',
127 8
                $min,
128 8
                $max,
129 8
                $this->createErrorMessage()
130
            )
131
        );
132
    }
133
134
    /**
135
     * @return string
136
     */
137 8
    private function createErrorMessage(): string
138
    {
139 8
        return implode(
140 8
            "\r\n",
141 8
            array_map(
142 8
                function ($values) {
143 8
                    return implode(' ', $values);
144 8
                },
145 8
                $this->lines
146
            )
147
        );
148
    }
149
}
150