Completed
Pull Request — master (#42)
by Frederik
01:50
created

Tag::extractBodyFromLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 0
cts 13
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 6
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
    public function __construct(string $tag)
23
    {
24
        $this->tag = $tag;
25
        $this->tagLength = strlen($tag);
26
    }
27
28
    /**
29
     * @return string
30
     */
31
    public function __toString(): string
32
    {
33
        return $this->tag;
34
    }
35
36
    /**
37
     * @param string $line
38
     * @return string
39
     */
40
    public function extractBodyFromLine(string $line): string
41
    {
42
        if (substr($line, 0, $this->tagLength + 1) === $this->tag . ' ') {
43
            return substr($line, $this->tagLength + 1);
44
        }
45
46
        throw new \InvalidArgumentException(
47
            sprintf(
48
                'Line `%s` does not contain the tag %s',
49
                $line,
50
                $this->tag
51
            )
52
        );
53
    }
54
}