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

FetchCommandResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 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
use Genkgo\Mail\Protocol\Imap\ResponseInterface;
8
9
/**
10
 * Class FetchCommandResponse
11
 * @package Genkgo\Mail\Protocol\Imap\Response\Command
12
 */
13
final class FetchCommandResponse
14
{
15
    /**
16
     * @var int
17
     */
18
    private $number;
19
    /**
20
     * @var ItemList
21
     */
22
    private $dataItemList;
23
24
    /**
25
     * FetchCommandResponse constructor.
26
     * @param int $number
27
     * @param ItemList $dataItemList
28
     */
29
    public function __construct(int $number, ItemList $dataItemList)
30
    {
31
        $this->number = $number;
32
        $this->dataItemList = $dataItemList;
33
    }
34
35
    /**
36
     * @return int
37
     */
38
    public function getNumber(): int
39
    {
40
        return $this->number;
41
    }
42
43
    /**
44
     * @return ItemList
45
     */
46
    public function getDataItemList(): ItemList
47
    {
48
        return $this->dataItemList;
49
    }
50
51
    /**
52
     * @param ResponseInterface $response
53
     * @return FetchCommandResponse
54
     */
55
    public static function fromResponse(ResponseInterface $response): self
56
    {
57
        $body = $response->getBody();
58
59
        $matches = [];
60
        $result = preg_match('/^([0-9]+) FETCH \((.*?)\)\s*$/s', $body, $matches);
61
        if ($result !== 1) {
62
            throw new \InvalidArgumentException('Not a fetch command');
63
        }
64
65
        return new self((int)$matches[1], ItemList::fromString($matches[2]));
66
    }
67
68
}