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

FetchCommandResponse   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 66
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNumber() 0 4 1
A getDataItemList() 0 4 1
A __toString() 0 8 1
A fromString() 0 10 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Response\Command;
5
6
use Genkgo\Mail\Protocol\Imap\MessageData\ItemList;
7
8
/**
9
 * Class FetchCommandResponse
10
 * @package Genkgo\Mail\Protocol\Imap\Response\Command
11
 */
12
final class FetchCommandResponse
13
{
14
    /**
15
     * @var int
16
     */
17
    private $number;
18
    /**
19
     * @var ItemList
20
     */
21
    private $dataItemList;
22
23
    /**
24
     * FetchCommandResponse constructor.
25
     * @param int $number
26
     * @param ItemList $dataItemList
27
     */
28 2
    public function __construct(int $number, ItemList $dataItemList)
29
    {
30 2
        $this->number = $number;
31 2
        $this->dataItemList = $dataItemList;
32 2
    }
33
34
    /**
35
     * @return int
36
     */
37 1
    public function getNumber(): int
38
    {
39 1
        return $this->number;
40
    }
41
42
    /**
43
     * @return ItemList
44
     */
45 1
    public function getDataItemList(): ItemList
46
    {
47 1
        return $this->dataItemList;
48
    }
49
50
    /**
51
     * @return string
52
     */
53 1
    public function __toString(): string
54
    {
55 1
        return sprintf(
56 1
            "%s FETCH %s",
57 1
            $this->number,
58 1
            (string)$this->dataItemList
59
        );
60
    }
61
62
    /**
63
     * @param string$response
64
     * @return FetchCommandResponse
65
     */
66 3
    public static function fromString(string $response): self
67
    {
68 3
        $matches = [];
69 3
        $result = preg_match('/^([0-9]+) FETCH ((\()?.*?(\))?)\s*$/s', $response, $matches);
70 3
        if ($result !== 1) {
71 1
            throw new \InvalidArgumentException('Not a fetch command');
72
        }
73
74 2
        return new self((int)$matches[1], ItemList::fromString($matches[2]));
75
    }
76
77
}