Completed
Push — master ( 596e7f...25001a )
by Ehsan
02:49
created

MessageUtility::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Slackbot\utility;
4
5
use Slackbot\CommandContainer;
6
7
/**
8
 * Class MessageUtility.
9
 */
10
class MessageUtility extends AbstractUtility
11
{
12
    /**
13
     * Remove the mentioned bot username from the message.
14
     *
15
     * @param $message
16
     *
17
     * @throws \Exception
18
     *
19
     * @return string
20
     */
21
    public function removeMentionedBot($message)
22
    {
23
        $botUserId = $this->getConfig()->get('botUserId');
24
        $userLink = $this->linkToUser($botUserId);
25
26
        return preg_replace("/{$userLink}/", '', $message, 1);
27
    }
28
29
    /**
30
     * Check if the bot user id is mentioned in the message.
31
     *
32
     * @param $message
33
     *
34
     * @throws \Exception
35
     *
36
     * @return bool
37
     */
38
    public function isBotMentioned($message)
39
    {
40
        $botUserId = $this->getConfig()->get('botUserId');
41
        $userLink = $this->linkToUser($botUserId);
42
43
        return (new StringUtility())->findInString($userLink, $message, false);
44
    }
45
46
    /**
47
     * Return command name in the message.
48
     *
49
     * @param $message
50
     *
51
     * @return null|string
52
     */
53
    public function extractCommandName($message)
54
    {
55
        // remove the bot mention if it exists
56
        $message = $this->removeMentionedBot($message);
57
58
        /**
59
         * Command must start with / and at the beginning of the sentence.
60
         */
61
        $commandPrefix = $this->getConfig()->get('commandPrefix');
62
        $commandPrefix = preg_quote($commandPrefix, '/');
63
64
        $pattern = '/^('.$commandPrefix.'\w{1,})/';
65
66
        preg_match($pattern, ltrim($message), $groups);
67
68
        // If command is found, remove command prefix from the beginning of the command
69
        return isset($groups[1]) ? ltrim($groups[1], $commandPrefix) : null;
70
    }
71
72
    /**
73
     * Return command details in the message.
74
     *
75
     * @param $message
76
     *
77
     * @return null
78
     */
79
    public function extractCommandDetails($message)
80
    {
81
        // first get the command name
82
        $command = $this->extractCommandName($message);
83
84
        // then get the command details
85
        return (new CommandContainer())->getAsObject($command);
86
    }
87
88
    /**
89
     * @param $triggerWord
90
     * @param $message
91
     *
92
     * @return string
93
     */
94
    public function removeTriggerWord($message, $triggerWord)
95
    {
96
        $count = 1;
97
98
        return ltrim(str_replace($triggerWord, '', $message, $count));
99
    }
100
101
    /**
102
     * @param        $userId
103
     * @param string $userName
104
     *
105
     * @throws \Exception
106
     *
107
     * @return string
108
     */
109
    public function linkToUser($userId, $userName = '')
110
    {
111
        if (empty($userId)) {
112
            throw new \Exception('User id is not provided');
113
        }
114
115
        if (!empty($userName)) {
116
            $userName = "|{$userName}";
117
        }
118
119
        return "<@{$userId}{$userName}>";
120
    }
121
}
122