CompletionResult   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 0
dl 0
loc 85
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A equals() 0 4 1
A __toString() 0 4 1
A fromString() 0 4 1
A fromLine() 0 10 2
A ok() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Response;
5
6
final class CompletionResult
7
{
8
    private const BAD = 'BAD';
9
    
10
    private const NO = 'NO';
11
    
12
    private const OK = 'OK';
13
    
14
    private const ENUM = [
15
        self::BAD => true,
16
        self::NO => true,
17
        self::OK => true,
18
    ];
19
20
    /**
21
     * @var string
22
     */
23
    private $result;
24
25
    /**
26
     * @param string $value
27
     */
28 26
    private function __construct(string $value)
29
    {
30 26
        if (!isset(self::ENUM[$value])) {
31 3
            throw new \InvalidArgumentException(
32 3
                \sprintf(
33 3
                    'Invalid completion result %s',
34 3
                    $value
35
                )
36
            );
37
        }
38
39 25
        $this->result = $value;
40 25
    }
41
42
    /**
43
     * @param CompletionResult $completionResult
44
     * @return bool
45
     */
46 18
    public function equals(CompletionResult $completionResult): bool
47
    {
48 18
        return $this->result === $completionResult->result;
49
    }
50
51
    /**
52
     * @return string
53
     */
54 6
    public function __toString(): string
55
    {
56 6
        return $this->result;
57
    }
58
59
    /**
60
     * @param string $result
61
     * @return CompletionResult
62
     */
63 3
    public static function fromString(string $result): self
64
    {
65 3
        return new self($result);
66
    }
67
68
    /**
69
     * @param string $line
70
     * @return CompletionResult
71
     */
72 20
    public static function fromLine(string $line): self
73
    {
74 20
        $found = \preg_match('/^([A-Z]{0,3})\s/', $line, $matches);
75
76 20
        if ($found === 0) {
77 14
            return new self($line);
78
        }
79
80 8
        return new self($matches[1]);
81
    }
82
83
    /**
84
     * @return CompletionResult
85
     */
86 24
    public static function ok(): self
87
    {
88 24
        return new self(self::OK);
89
    }
90
}
91