Completed
Push — master ( 3ff097...503b9d )
by Frederik
06:54
created

Reply::assertBetween()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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