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

Tag   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 49
c 0
b 0
f 0
ccs 0
cts 22
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __toString() 0 4 1
A extractBodyFromLine() 0 14 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
    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
}