MatcherTrait::matches()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
namespace Nopolabs\Yabot\Helpers;
4
5
6
use Nopolabs\Yabot\Message\Message;
7
8
trait MatcherTrait
9
{
10
    private $name;
11
    private $isBot;
12
    private $channels;
13
    private $users;
14
    private $patterns;
15
16
    abstract protected function debug($message, array $context = array());
17
18
    public function matches(Message $message) : array
19
    {
20
        if (!$this->matchesIsBot($message)) {
21
            return [];
22
        }
23
24
        if (!$this->matchesChannels($message)) {
25
            return [];
26
        }
27
28
        if (!$this->matchesUsers($message)) {
29
            return [];
30
        }
31
32
        if (empty($this->patterns)) {
33
            return [$message->getFormattedText()];
34
        }
35
36
        return $this->matchPatterns($message);
37
    }
38
39
    protected function matchesIsBot(Message $message) : bool
40
    {
41
        if ($this->isBot !== null && $this->isBot !== $message->isBot()) {
42
            $this->debug($this->name.': isBot match failed');
43
            return false;
44
        }
45
46
        return true;
47
    }
48
49 View Code Duplication
    protected function matchesChannels(Message $message) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        if (!empty($this->channels) && !in_array($message->getChannelName(), $this->channels)) {
52
            $this->debug($this->name.': channels match failed '.json_encode($this->channels));
53
            return false;
54
        }
55
56
        return true;
57
    }
58
59 View Code Duplication
    protected function matchesUsers(Message $message) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        if (!empty($this->users) && !in_array($message->getUsername(), $this->users)) {
62
            $this->debug($this->name.': users match failed '.json_encode($this->users));
63
            return false;
64
        }
65
66
        return true;
67
    }
68
69
    protected function matchPatterns(Message $message): array
70
    {
71
        $matches = [];
72
        $text = $message->getPluginText();
73
        foreach ($this->patterns as $pattern) {
74
            if (preg_match($pattern, $text, $matches)) {
75
                break;
76
            }
77
        }
78
79
        return $matches;
80
    }
81
82
    private function setName($name)
83
    {
84
        $this->name = $name;
85
    }
86
87
    private function setIsBot($isBot)
88
    {
89
        $this->isBot = $isBot;
90
    }
91
92
    private function setChannels(array $channels)
93
    {
94
        $this->channels = $channels;
95
    }
96
97
    private function setUsers(array $users)
98
    {
99
        $this->users = $users;
100
    }
101
102
    private function setPatterns(array $patterns)
103
    {
104
        $this->patterns = $patterns;
105
    }
106
}