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

FetchCommandResponse   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 56
c 0
b 0
f 0
ccs 0
cts 23
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNumber() 0 4 1
A getDataItemList() 0 4 1
A fromResponse() 0 12 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
}