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

Partial   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A __toString() 0 7 2
A fromString() 0 10 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\MessageData;
5
6
final class Partial
7
{
8
    /**
9
     * @var int
10
     */
11
    private $firstByte;
12
    /**
13
     * @var int
14
     */
15
    private $lastByte;
16
17
    /**
18
     * @param int $firstByte
19
     * @param int $lastByte
20
     */
21 8
    public function __construct(int $firstByte, int $lastByte)
22
    {
23 8
        if ($firstByte < 0 || $lastByte < $firstByte) {
24 2
            throw new \InvalidArgumentException(
25 2
                'Last byte must be greater than first byte, and first byte must be greater than zero'
26
            );
27
        }
28
29 6
        $this->firstByte = $firstByte;
30 6
        $this->lastByte = $lastByte;
31 6
    }
32
33
    /**
34
     * @return string
35
     */
36 5
    public function __toString(): string
37
    {
38 5
        return sprintf(
39 5
            '<%s>',
40 5
            $this->firstByte === $this->lastByte ? $this->firstByte : $this->firstByte . '.' . $this->lastByte
41
        );
42
    }
43
44
    /**
45
     * @param string $partial
46
     * @return Partial
47
     */
48 3
    public static function fromString(string $partial): self
49
    {
50 3
        $result = preg_match('/^<([0-9]+)(\.([0-9]+))?>$/', $partial, $matches);
51
52 3
        if ($result !== 1) {
53 1
            throw new \InvalidArgumentException('String is not a partial');
54
        }
55
56 2
        return new self((int)$matches[1], (int)($matches[3] ?? (int)$matches[1]));
57
    }
58
}