Passed
Pull Request — master (#49)
by Evgeniy
01:52
created

MessageCategory::checkStructure()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Log;
6
7
use Psr\Log\InvalidArgumentException;
8
9
use function gettype;
10
use function is_string;
11
use function rtrim;
12
use function substr_compare;
13
use function sprintf;
14
use function strpos;
15
16
/**
17
 * Category is a data object that stores and matches the included and excluded categories of log messages.
18
 */
19
final class MessageCategory
20
{
21
    public const DEFAULT = 'application';
22
23
    /**
24
     * @var string[] The list of included log message categories.
25
     *
26
     * Defaults to empty, which means all categories are included.
27
     *
28
     * You can use an asterisk at the end of a category so that the category may be used to
29
     * match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match
30
     * categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`.
31
     */
32
    private array $include = [];
33
34
    /**
35
     * @var string[] The list of excluded log message categories.
36
     *
37
     * Defaults to empty, which means there are no excluded categories.
38
     *
39
     * You can use an asterisk at the end of a category so that the category can be used to
40
     * match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match
41
     * categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`.
42
     */
43
    private array $exclude = [];
44
45
    /**
46
     * Sets the log message categories to be included.
47
     *
48
     * @param string[] $categories List of log message categories to be included.
49
     * @throws InvalidArgumentException for invalid log message categories structure.
50
     */
51 28
    public function include(array $categories): void
52
    {
53 28
        $this->checkStructure($categories);
54 16
        $this->include = $categories;
55 16
    }
56
57
    /**
58
     * Gets the log message categories to be included.
59
     *
60
     * @return string[] The list of log message categories to be included.
61
     */
62 2
    public function getIncluded(): array
63
    {
64 2
        return $this->include;
65
    }
66
67
    /**
68
     * Sets the log message categories to be excluded.
69
     *
70
     * @param string[] $categories The list of log message categories to be excluded.
71
     * @throws InvalidArgumentException for invalid log message categories structure.
72
     */
73 19
    public function exclude(array $categories): void
74
    {
75 19
        $this->checkStructure($categories);
76 7
        $this->exclude = $categories;
77 7
    }
78
79
    /**
80
     * Gets the log message categories to be excluded.
81
     *
82
     * @return string[] The list of log message categories to be excluded.
83
     */
84 2
    public function getExcluded(): array
85
    {
86 2
        return $this->exclude;
87
    }
88
89
    /**
90
     * Checks whether the specified log message category is excluded.
91
     *
92
     * @param string $category The log message category.
93
     * @return bool The value indicating whether the specified category is excluded.
94
     */
95 31
    public function isExcluded(string $category): bool
96
    {
97 31
        foreach ($this->exclude as $exclude) {
98 5
            $prefix = rtrim($exclude, '*');
99
100 5
            if ($category === $exclude || ($prefix !== $exclude && strpos($category, $prefix) === 0)) {
101 5
                return true;
102
            }
103
        }
104
105 31
        if (empty($this->include)) {
106 17
            return false;
107
        }
108
109 14
        foreach ($this->include as $include) {
110
            if (
111 14
                $category === $include
112
                || (
113 14
                    !empty($include)
114 14
                    && substr_compare($include, '*', -1, 1) === 0
115 14
                    && strpos($category, rtrim($include, '*')) === 0
116
                )
117
            ) {
118 13
                return false;
119
            }
120
        }
121
122 14
        return true;
123
    }
124
125
    /**
126
     * Checks message categories structure.
127
     *
128
     * @param array $categories The log message categories to be checked.
129
     * @throws InvalidArgumentException for invalid log message categories structure.
130
     */
131 43
    private function checkStructure(array $categories): void
132
    {
133 43
        foreach ($categories as $category) {
134 43
            if (!is_string($category)) {
135 24
                throw new InvalidArgumentException(sprintf(
136 24
                    'The log message category must be a string, %s received.',
137 24
                    gettype($category)
138
                ));
139
            }
140
        }
141 19
    }
142
}
143