Completed
Pull Request — master (#42)
by Frederik
02:56
created

CapabilityCommandResponse::fromResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
ccs 0
cts 16
cp 0
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Response\Command;
5
6
use Genkgo\Mail\Protocol\Imap\ResponseInterface;
7
8
final class CapabilityCommandResponse
9
{
10
    /**
11
     * @var array
12
     */
13
    private $advertisements = [];
14
15
    /**
16
     * CapabilityList constructor.
17
     * @param array $list
18
     */
19
    public function __construct(array $list)
20
    {
21
        $this->advertisements = $list;
22
    }
23
24
    /**
25
     * @param string $command
26
     * @return bool
27
     */
28
    public function isAdvertising(string $command)
29
    {
30
        if ($command === '') {
31
            return false;
32
        }
33
34
        return isset($this->advertisements[$command]);
35
    }
36
37
    /**
38
     * @param ResponseInterface $response
39
     * @return CapabilityCommandResponse
40
     */
41
    public static function fromResponse(ResponseInterface $response): self
42
    {
43
        $command = 'CAPABILITY ';
44
        $commandLength = strlen($command);
45
46
        $body = $response->getBody();
47
        if (substr($body,0, $commandLength) !== $command) {
48
            throw new \InvalidArgumentException('Expected CAPABILITY command');
49
        }
50
51
        $advertisements = preg_split('/[\s]+/', substr($body, $commandLength));
52
53
        return new self(
54
            array_combine(
55
                $advertisements,
56
                array_fill(0, count($advertisements), true)
57
            )
58
        );
59
    }
60
61
}