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

CapabilityCommandResponse::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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