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

CapabilityCommandResponse   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 54
c 0
b 0
f 0
ccs 0
cts 27
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isAdvertising() 0 8 2
A fromResponse() 0 19 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
    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
}