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

Partial   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 47
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
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
    public function __construct(int $firstByte, int $lastByte)
22
    {
23
        $this->firstByte = $firstByte;
24
        $this->lastByte = $lastByte;
25
    }
26
27
    /**
28
     * @return string
29
     */
30
    public function __toString(): string
31
    {
32
        return sprintf(
33
            '<%s>',
34
            $this->firstByte === $this->lastByte ? $this->firstByte : $this->firstByte . '.' . $this->lastByte
35
        );
36
    }
37
38
    /**
39
     * @param string $partial
40
     * @return Partial
41
     */
42
    public static function fromString(string $partial): self
43
    {
44
        $result = preg_match('/^<([0-9]+)\.([0-9]+)$/', $partial, $matches);
45
46
        if ($result !== 1) {
47
            throw new \InvalidArgumentException('String is not a partial');
48
        }
49
50
        return new self((int)$matches[1], (int)$matches[2]);
51
    }
52
}