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

FlagsItem::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\MessageData\Item;
5
6
use Genkgo\Mail\Protocol\Imap\FlagParenthesizedList;
7
use Genkgo\Mail\Protocol\Imap\MessageData\ItemInterface;
8
9
/**
10
 * Class BodySection
11
 * @package Genkgo\Mail\Protocol\Imap\MessageData\GenericItem
12
 */
13
final class FlagsItem implements ItemInterface
14
{
15
    /**
16
     *
17
     */
18
    public CONST OPERATOR_REPLACE = '';
19
    /**
20
     *
21
     */
22
    public CONST OPERATOR_ADD = '+';
23
    /**
24
     *
25
     */
26
    public CONST OPERATOR_REMOVE = '-';
27
    /**
28
     *
29
     */
30
    private CONST OPERATORS = [
31
        self::OPERATOR_REPLACE => true,
32
        self::OPERATOR_ADD => true,
33
        self::OPERATOR_REMOVE => true,
34
    ];
35
    /**
36
     * @var FlagParenthesizedList
37
     */
38
    private $flagList;
39
40
    /**
41
     * @var bool
42
     */
43
    private $silent = false;
44
45
    /**
46
     * @var string
47
     */
48
    private $operator = '';
49
50
    /**
51
     * BodySection constructor.
52
     * @param FlagParenthesizedList $flagList
53
     * @param string $operator
54
     */
55 2
    public function __construct(FlagParenthesizedList $flagList, string $operator = '')
56
    {
57 2
        if (!isset(self::OPERATORS[$operator])) {
58
            throw new \InvalidArgumentException('Invalid operator');
59
        }
60
61 2
        $this->flagList = $flagList;
62 2
        $this->operator = $operator;
63 2
    }
64
65
    /**
66
     * @return string
67
     */
68 2
    public function getName(): string
69
    {
70 2
        return $this->operator . $this->silent ? 'FLAGS.SILENT' : 'FLAGS';
71
    }
72
73
    /**
74
     * @return string
75
     */
76 1
    public function __toString(): string
77
    {
78 1
        return sprintf(
79 1
            '%s %s',
80 1
            $this->getName(),
81 1
            (string)$this->flagList
82
        );
83
    }
84
85
    /**
86
     * @param FlagParenthesizedList $flagParenthesizedList
87
     * @param string $operator
88
     * @return FlagsItem
89
     */
90
    public static function silent(FlagParenthesizedList $flagParenthesizedList, string $operator = ''): self
91
    {
92
        $bodySection = new self($flagParenthesizedList, $operator);
93
        $bodySection->silent = true;
94
        return $bodySection;
95
    }
96
}