Completed
Pull Request — master (#12)
by Frederik
01:58
created

Reply   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 125
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isError() 0 9 2
A getMessages() 0 4 1
A withLine() 0 8 1
A assert() 0 3 1
A assertCompleted() 0 3 1
A assertIntermediate() 0 3 1
A createErrorMessage() 0 12 1
A assertBetween() 0 17 4
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 23
    public function __construct(Client $client)
36
    {
37 23
        $this->client = $client;
38 23
    }
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 array
55
     */
56 13
    public function getMessages(): array
57
    {
58 13
        return $this->messages;
59
    }
60
61
    /**
62
     * @param int $code
63
     * @param string $message
64
     * @return Reply
65
     */
66 23
    public function withLine(int $code, string $message): Reply
67
    {
68 23
        $clone = clone $this;
69 23
        $clone->lines[] = [$code, $message];
70 23
        $clone->codes[$code] = true;
71 23
        $clone->messages[] = $message;
72 23
        return $clone;
73
    }
74
75
    /**
76
     * @param int $code
77
     * @return Client
78
     */
79 1
    public function assert(int $code): Client {
80 1
        return $this->assertBetween($code, $code);
81
    }
82
83
    /**
84
     * @return Client
85
     */
86 13
    public function assertCompleted(): Client {
87 13
        return $this->assertBetween(200, 299);
88
    }
89
90
    /**
91
     * @return Client
92
     */
93 6
    public function assertIntermediate(): Client {
94 6
        return $this->assertBetween(300, 399);
95
    }
96
97
    /**
98
     * @param int $min
99
     * @param int $max
100
     * @return Client
101
     * @throws AssertionFailedException
102
     */
103 18
    private function assertBetween(int $min, int $max): Client
104
    {
105 18
        foreach ($this->codes as $code => $activated) {
106 18
            if ($code >= $min && $code <= $max) {
107 16
                return $this->client;
108
            }
109
        }
110
111 5
        throw new AssertionFailedException(
112 5
            sprintf(
113 5
                'Cannot assert reply code between %s and %s. Server replied %s.',
114 5
                $min,
115 5
                $max,
116 5
                $this->createErrorMessage()
117
            )
118
        );
119
    }
120
121
    /**
122
     * @return string
123
     */
124 5
    private function createErrorMessage(): string
125
    {
126 5
        return implode(
127 5
            "\r\n",
128 5
            array_map(
129 5
                function ($values) {
130 5
                    return implode(' ', $values);
131 5
                },
132 5
                $this->lines
133
            )
134
        );
135
    }
136
}