Completed
Push — master ( c2c2ca...b018a7 )
by Dan
02:22
created

MatcherTrait::matchesChannels()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 3
eloc 5
nc 2
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)
19
    {
20
        if (!$this->matchesIsHandled($message)) {
21
            return false;
22
        }
23
24
        if (!$this->matchesIsBot($message)) {
25
            return false;
26
        }
27
28
        if (!$this->matchesChannels($message)) {
29
            return false;
30
        }
31
32
        if (!$this->matchesUsers($message)) {
33
            return false;
34
        }
35
36
        if (empty($this->patterns)) {
37
            return true;
38
        }
39
40
        return $this->matchPatterns($message);
41
    }
42
43
    protected function matchesIsHandled(Message $message) : bool
44
    {
45
        if ($message->isHandled()) {
46
            $this->debug($this->name.': message already handled');
47
            return false;
48
        }
49
50
        return true;
51
    }
52
53
    protected function matchesIsBot(Message $message) : bool
54
    {
55
        if ($this->isBot !== null && $this->isBot !== $message->isBot()) {
56
            $this->debug($this->name.': isBot match failed');
57
            return false;
58
        }
59
60
        return true;
61
    }
62
63 View Code Duplication
    protected function matchesChannels(Message $message) : bool
1 ignored issue
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...
64
    {
65
        if (!empty($this->channels) && !in_array($message->getChannelName(), $this->channels)) {
66
            $this->debug($this->name.': channels match failed '.json_encode($this->channels));
67
            return false;
68
        }
69
70
        return true;
71
    }
72
73 View Code Duplication
    protected function matchesUsers(Message $message) : bool
1 ignored issue
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...
74
    {
75
        if (!empty($this->users) && !in_array($message->getUsername(), $this->users)) {
76
            $this->debug($this->name.': users match failed '.json_encode($this->users));
77
            return false;
78
        }
79
80
        return true;
81
    }
82
83
    protected function matchPatterns(Message $message): array
84
    {
85
        $matches = [];
86
        $text = $message->getPluginText();
87
        foreach ($this->patterns as $pattern) {
88
            if (preg_match($pattern, $text, $matches)) {
89
                break;
90
            }
91
        }
92
93
        return $matches;
94
    }
95
96
    private function setName($name)
97
    {
98
        $this->name = $name;
99
    }
100
101
    private function setIsBot($isBot)
102
    {
103
        $this->isBot = $isBot;
104
    }
105
106
    private function setChannels(array $channels)
107
    {
108
        $this->channels = $channels;
109
    }
110
111
    private function setUsers(array $users)
112
    {
113
        $this->users = $users;
114
    }
115
116
    private function setPatterns(array $patterns)
117
    {
118
        $this->patterns = $patterns;
119
    }
120
}