Completed
Push — 2.1 ( 4d9204...3e6f8b )
by
unknown
11:56
created

Target::filterMessages()   C

Complexity

Conditions 13
Paths 25

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 13

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 16
nc 25
nop 1
crap 13

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\profile;
9
10
use yii\base\Component;
11
12
/**
13
 * Target is the base class for all profiling target classes.
14
 *
15
 * A profile target object will filter the messages stored by [[Profiler]] according
16
 * to its [[categories]] and [[except]] properties.
17
 *
18
 * For more details and usage information on Target, see the [guide article on profiling & targets](guide:runtime-profiling).
19
 *
20
 * @author Paul Klimov <[email protected]>
21
 * @since 2.1
22
 */
23
abstract class Target extends Component
24
{
25
    /**
26
     * @var bool whether to enable this log target. Defaults to true.
27
     */
28
    public $enabled = true;
29
    /**
30
     * @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories.
31
     * You can use an asterisk at the end of a category so that the category may be used to
32
     * match those categories sharing the same common prefix. For example, 'yii\db\*' will match
33
     * categories starting with 'yii\db\', such as `yii\db\Connection`.
34
     */
35
    public $categories = [];
36
    /**
37
     * @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no uninteresting messages.
38
     * If this property is not empty, then any category listed here will be excluded from [[categories]].
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, 'yii\db\*' will match
41
     * categories starting with 'yii\db\', such as `yii\db\Connection`.
42
     * @see categories
43
     */
44
    public $except = [];
45
46
47
    /**
48
     * Processes the given log messages.
49
     * This method will filter the given messages with [[levels]] and [[categories]].
50
     * And if requested, it will also export the filtering result to specific medium (e.g. email).
51
     * @param array $messages profiling messages to be processed. See [[Logger::messages]] for the structure
52
     * of each message.
53
     */
54 1
    public function collect(array $messages)
55
    {
56 1
        if (!$this->enabled) {
57 1
            return;
58
        }
59
60 1
        $messages = $this->filterMessages($messages);
61 1
        if (count($messages) > 0) {
62 1
            $this->export($messages);
63
        }
64 1
    }
65
66
    /**
67
     * Exports profiling messages to a specific destination.
68
     * Child classes must implement this method.
69
     * @param array $messages profiling messages to be exported.
70
     */
71
    abstract public function export(array $messages);
72
73
    /**
74
     * Filters the given messages according to their categories.
75
     * @param array $messages messages to be filtered.
76
     * The message structure follows that in [[Logger::messages]].
77
     * @return array the filtered messages.
78
     */
79 6
    protected function filterMessages($messages)
80
    {
81 6
        foreach ($messages as $i => $message) {
82 6
            $matched = empty($this->categories);
83 6
            foreach ($this->categories as $category) {
84 2
                if ($message['category'] === $category || !empty($category) && substr_compare($category, '*', -1, 1) === 0 && strpos($message['category'], rtrim($category, '*')) === 0) {
85 1
                    $matched = true;
86 2
                    break;
87
                }
88
            }
89
90 6
            if ($matched) {
91 5
                foreach ($this->except as $category) {
92 2
                    $prefix = rtrim($category, '*');
93 2
                    if (($message['category'] === $category || $prefix !== $category) && strpos($message['category'], $prefix) === 0) {
94 1
                        $matched = false;
95 2
                        break;
96
                    }
97
                }
98
            }
99
100 6
            if (!$matched) {
101 6
                unset($messages[$i]);
102
            }
103
        }
104 6
        return $messages;
105
    }
106
}