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

MatchContentCriterion   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 66
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A __toString() 0 8 1
A body() 0 4 1
A text() 0 4 1
A subject() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Request\SearchCriteria;
5
6
final class MatchContentCriterion implements CriterionInterface
7
{
8
9
    /**
10
     * @var string
11
     */
12
    private $query;
13
    /**
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * BodyCriterium constructor.
20
     * @param string $name
21
     * @param string $query
22
     */
23 17
    private function __construct(string $name, string $query)
24
    {
25 17
        if ($query === '' || strlen($query) !== strcspn($query, "\r\n")) {
26 3
            throw new \InvalidArgumentException('CR and LF are not allowed in quoted strings');
27
        }
28
29 14
        $this->name = $name;
30 14
        $this->query = $query;
31 14
    }
32
33
    /**
34
     * @return string
35
     */
36 10
    public function __toString(): string
37
    {
38 10
        return sprintf(
39 10
            '%s "%s"',
40 10
            $this->name,
41 10
            addslashes($this->query)
42
        );
43
    }
44
45
    /**
46
     * @param string $query
47
     * @return MatchContentCriterion
48
     */
49 6
    public static function body(string $query): self
50
    {
51 6
        return new self('BODY', $query);
52
    }
53
54
    /**
55
     * @param string $query
56
     * @return MatchContentCriterion
57
     */
58 10
    public static function text(string $query): self
59
    {
60 10
        return new self('TEXT', $query);
61
    }
62
63
    /**
64
     * @param string $query
65
     * @return MatchContentCriterion
66
     */
67 1
    public static function subject(string $query): self
68
    {
69 1
        return new self('SUBJECT', $query);
70
    }
71
}