Completed
Pull Request — master (#42)
by Frederik
02:26
created

Tag   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 58
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __toString() 0 4 1
A extractBodyFromLine() 0 14 2
A fromNonce() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap;
5
6
final class Tag
7
{
8
9
    /**
10
     * @var string
11
     */
12
    private $tag;
13
    /**
14
     * @var int
15
     */
16
    private $tagLength;
17
18
    /**
19
     * Tag constructor.
20
     * @param string $tag
21
     */
22 74
    public function __construct(string $tag)
23
    {
24 74
        $this->tag = $tag;
25 74
        $this->tagLength = strlen($tag);
26 74
    }
27
28
    /**
29
     * @return string
30
     */
31 43
    public function __toString(): string
32
    {
33 43
        return $this->tag;
34
    }
35
36
    /**
37
     * @param string $line
38
     * @return string
39
     */
40 18
    public function extractBodyFromLine(string $line): string
41
    {
42 18
        if (substr($line, 0, $this->tagLength + 1) === $this->tag . ' ') {
43 16
            return substr($line, $this->tagLength + 1);
44
        }
45
46 3
        throw new \InvalidArgumentException(
47 3
            sprintf(
48 3
                'Line `%s` does not contain the tag %s',
49 3
                $line,
50 3
                $this->tag
51
            )
52
        );
53
    }
54
55
    /**
56
     * @param int $nonce
57
     * @return Tag
58
     */
59 73
    public static function fromNonce(int $nonce): self
60
    {
61 73
        return new self('TAG' . $nonce);
62
    }
63
}