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

FlagCriterion   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 14 3
A negate() 0 6 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Request\SearchCriteria;
5
6
use Genkgo\Mail\Protocol\Imap\Flag;
7
8
/**
9
 * Class FlagCriterion
10
 * @package Genkgo\Mail\Protocol\Imap\Request\SearchCriteria
11
 */
12
final class FlagCriterion implements CriterionInterface
13
{
14
    /**
15
     * @var
16
     */
17
    private $flag;
18
    /**
19
     * @var bool
20
     */
21
    private $negate = false;
22
23
    /**
24
     * FlagCriterion constructor.
25
     * @param Flag $flag
26
     */
27 4
    public function __construct(Flag $flag)
28
    {
29 4
        $this->flag = $flag;
30 4
    }
31
32
    /**
33
     * @return string
34
     */
35 4
    public function __toString(): string
36
    {
37 4
        $prefix = $this->negate ? 'UN' : '';
38
39 4
        if ($this->flag->isKeyword()) {
40 2
            return \sprintf(
41 2
                '%sKEYWORD %s',
42 2
                $prefix,
43 2
                (string)$this->flag
44
            );
45
        }
46
47 2
        return $prefix . \strtoupper(substr((string)$this->flag, 1));
48
    }
49
50
    /**
51
     * @param Flag $flag
52
     * @return FlagCriterion
53
     */
54 2
    public static function negate(Flag $flag): self
55
    {
56 2
        $criterion = new self($flag);
57 2
        $criterion->negate = true;
58 2
        return $criterion;
59
    }
60
}