DispatcherTools   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A orderedListeners() 0 14 3
A match() 0 6 1
1
<?php
2
3
/**
4
 * This file is part of Event
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Event\Application;
13
14
/**
15
 * DispatcherTools
16
 *
17
 * @package Slick\Event\Application
18
 */
19
trait DispatcherTools
20
{
21
    /**
22
     * Matches the listener register pattern with event
23
     *
24
     * @param $pattern
25
     * @param object $event
26
     * @return bool
27
     */
28
    protected function match($pattern, object $event): bool
29
    {
30
        $regEx = str_replace(['\\', '*'], ['\\\\', '(.*)'], $pattern);
31
        $regEx = "/$regEx/i";
32
        $name = get_class($event);
33
        return (bool) preg_match($regEx, $name);
34
    }
35
36
    /**
37
     * orderedListeners
38
     *
39
     * @param array $unordered
40
     * @return array
41
     */
42
    protected function orderedListeners(array $unordered): array
43
    {
44
        usort($unordered, function ($a, $b) {
45
            if ($a->priority > $b->priority) {
46
                return -1;
47
            }
48
49
            if ($a->priority === $b->priority) {
50
                return 0;
51
            }
52
53
            return 1;
54
        });
55
        return $unordered;
56
    }
57
}
58