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

Tag::extractBodyFromLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2
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
}