Completed
Pull Request — master (#42)
by Frederik
03:39
created

CompletionResult::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 0
cts 12
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Response;
5
6
/**
7
 * Class CompletionResult
8
 * @package Genkgo\Mail\Protocol\Imap\Response
9
 */
10
final class CompletionResult
11
{
12
    /**
13
     *
14
     */
15
    private CONST BAD = 'BAD';
16
    /**
17
     *
18
     */
19
    private CONST NO = 'NO';
20
    /**
21
     *
22
     */
23
    private CONST OK = 'OK';
24
    /**
25
     *
26
     */
27
    private CONST ENUM = [
28
        self::BAD => true,
29
        self::NO => true,
30
        self::OK => true,
31
    ];
32
    /**
33
     * @var string
34
     */
35
    private $result;
36
37
    /**
38
     * CompletionResult constructor.
39
     * @param string $value
40
     */
41
    private function __construct(string $value)
42
    {
43
        if (!isset(self::ENUM[$value])) {
44
            throw new \InvalidArgumentException(
45
                sprintf(
46
                    'Invalid completion result %s',
47
                    $value
48
                )
49
            );
50
        }
51
52
        $this->result = $value;
53
    }
54
55
    /**
56
     * @param CompletionResult $completionResult
57
     * @return bool
58
     */
59
    public function equals(CompletionResult $completionResult): bool
60
    {
61
        return $this->result === $completionResult->result;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function __toString(): string
68
    {
69
        return $this->result;
70
    }
71
72
    /**
73
     * @param string $result
74
     * @return CompletionResult
75
     */
76
    public static function fromString(string $result): self
77
    {
78
        return new self($result);
79
    }
80
81
    /**
82
     * @param string $line
83
     * @return CompletionResult
84
     */
85
    public static function fromLine(string $line): self
86
    {
87
        $space = strpos($line, ' ');
88
89
        if ($space === false) {
90
            return new self($line);
91
        }
92
93
        return new self(substr($line, 0, $space));
94
    }
95
96
    /**
97
     * @return CompletionResult
98
     */
99
    public static function ok(): self
100
    {
101
        return new self(self::OK);
102
    }
103
}