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

CapabilityCommandResponse   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 56
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isAdvertising() 0 8 2
A fromResponse() 0 21 2
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 4
    public function __construct(array $list)
20
    {
21 4
        $this->advertisements = $list;
22 4
    }
23
24
    /**
25
     * @param string $command
26
     * @return bool
27
     */
28 4
    public function isAdvertising(string $command)
29
    {
30 4
        if ($command === '') {
31
            return false;
32
        }
33
34 4
        return isset($this->advertisements[$command]);
35
    }
36
37
    /**
38
     * @param ResponseInterface $response
39
     * @return CapabilityCommandResponse
40
     */
41 4
    public static function fromResponse(ResponseInterface $response): self
42
    {
43 4
        $command = 'CAPABILITY ';
44 4
        $commandLength = strlen($command);
45
46 4
        $body = $response->getBody();
47 4
        if (substr($body,0, $commandLength) !== $command) {
48
            throw new \InvalidArgumentException(
49
                sprintf('Expected CAPABILITY command, got %s', $body)
50
            );
51
        }
52
53 4
        $advertisements = preg_split('/[\s]+/', substr($body, $commandLength));
54
55 4
        return new self(
56 4
            array_combine(
57 4
                $advertisements,
58 4
                array_fill(0, count($advertisements), true)
59
            )
60
        );
61
    }
62
63
}