Completed
Branch master (812a3e)
by Frank
04:30 queued 02:09
created

TellCommand::processReviewMessage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 2
dl 0
loc 18
ccs 13
cts 13
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * T3Bot.
4
 *
5
 * @author Frank Nägler <[email protected]>
6
 *
7
 * @link http://www.t3bot.de
8
 * @link http://wiki.typo3.org/T3Bot
9
 */
10
namespace T3Bot\Commands;
11
12
use Slack\Payload;
13
use Slack\RealTimeClient;
14
15
/**
16
 * Class TellCommand.
17
 *
18
 * @property array helpCommands
19
 * @property string commandName
20
 */
21
class TellCommand extends AbstractCommand
22
{
23
    const PRESENCE_ACTIVE = 'active';
24
    const PRESENCE_AWAY = 'away';
25
26
    /**
27
     * AbstractCommand constructor.
28
     *
29
     * @param Payload        $payload
30
     * @param RealTimeClient $client
31
     */
32 8 View Code Duplication
    public function __construct(Payload $payload, RealTimeClient $client)
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...
33
    {
34 8
        $this->commandName = 'tell';
35 8
        $this->helpCommands = [
36
            'help' => 'shows this help',
37
            'tell [@to-user] about review:[Gerrit-ID]' => 'tell the target user about the given review',
38
            'tell [@to-user] about forge:[Issue-ID]' => 'tell the target user about the given forge issue',
39
            'tell [@to-user] [your message]' => 'tell the target user your message',
40
        ];
41 8
        parent::__construct($payload, $client);
42 8
    }
43
44
    /**
45
     * @return bool|string
46
     *
47
     * @throws \Doctrine\DBAL\DBALException
48
     */
49 6
    public function process()
50
    {
51 6
        $message = $this->payload->getData()['text'];
52 6
        $this->params = array_map('trim', explode(' ', $message));
53 6
        $result = false;
54 6
        if ($this->params[0] === 'tell') {
55 6
            $result = $this->processTell();
56
        }
57
58 6
        return $result;
59
    }
60
61
    /**
62
     * @param string $user
63
     * @param string $presence
64
     *
65
     * @throws \Doctrine\DBAL\DBALException
66
     */
67 4
    public function processPresenceChange($user, $presence)
68
    {
69 4
        if ($presence === self::PRESENCE_ACTIVE) {
70 3
            $queryBuilder = $this->getDatabaseConnection()->createQueryBuilder();
71
            $notifications = $queryBuilder
72 3
                ->select('*')
73 3
                ->from('notifications')
74 3
                ->where($queryBuilder->expr()->eq('to_user', $queryBuilder->createNamedParameter($user)))
75 3
                ->andWhere($queryBuilder->expr()->eq('delivered', $queryBuilder->createNamedParameter('0000-00-00 00:00:00')))
76 3
                ->execute()
77 3
                ->fetchAll();
78 3
            foreach ($notifications as $notification) {
79 3
                if (strpos($notification['message'], 'review:') === 0) {
80 1
                    $this->processReviewMessage($notification, $user);
81 2
                } elseif (strpos($notification['message'], 'forge:') === 0) {
82 1
                    $this->processForgeMessage($notification, $user);
83 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84 1
                    $msg = '*Hi <@' . $user . '>, here is a message from <@' . $notification['from_user'] . '>'
85 1
                        . ' for you:*';
86 1
                    $this->sendResponse($msg . "\n" . $notification['message'], $user);
87
                }
88 3
                $now = new \DateTime();
89 3
                $now->setTimestamp(time());
90 3
                $this->getDatabaseConnection()->update(
91 3
                    'notifications',
92 3
                    ['delivered' => $now],
93 3
                    ['id' => $notification['id']],
94 3
                    ['datetime']
95
                );
96
            }
97
        }
98 4
    }
99
100
    /**
101
     * @param array $notification
102
     * @param string $user
103
     */
104 1
    protected function processReviewMessage(array $notification, string $user)
105
    {
106 1
        $parts = explode(':', $notification['message']);
107 1
        $refId = (int) trim($parts[1]);
108 1
        $result = $this->queryGerrit('change:' . $refId);
109 1
        $msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>'
110 1
            . ' ask you to look at this patch:*';
111
112 1
        if (is_array($result)) {
113 1
            foreach ($result as $item) {
114 1
                if ((int) $item->_number === $refId) {
115 1
                    $message = $this->buildReviewMessage($item);
116 1
                    $message->setText($msg);
117 1
                    $this->sendResponse($message, $user);
118
                }
119
            }
120
        }
121 1
    }
122
123
    /**
124
     * @param array $notification
125
     * @param string $user
126
     */
127 1
    protected function processForgeMessage(array $notification, string $user)
128
    {
129 1
        $parts = explode(':', $notification['message']);
130 1
        $issueNumber = (int) trim($parts[1]);
131 1
        $result = $this->queryForge('issues/' . $issueNumber);
132 1 View Code Duplication
        if ($result) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
133 1
            $msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>'
134 1
                . ' ask you to look at this issue:*';
135 1
            $this->sendResponse($msg . "\n" . $this->buildIssueMessage($result->issue), $user);
136
        }
137 1
    }
138
139
    /**
140
     * @return string
141
     *
142
     * @throws \Doctrine\DBAL\DBALException
143
     */
144 6
    protected function processTell() : string
145
    {
146 6
        $params = $this->params;
147 6
        array_shift($params);
148 6
        $toUser = array_shift($params);
149 6
        $toUser = str_replace(['<', '>', '@'], '', $toUser);
150 6
        if ($params[0] === 'about'
151 6
            && (strpos($params[1], 'review:') !== false || strpos($params[1], 'forge:') !== false)) {
152 4
            $message = $params[1];
153
        } else {
154 2
            $message = implode(' ', $params);
155
        }
156 6
        $this->getDatabaseConnection()->insert('notifications', [
157 6
            'from_user' => $this->payload->getData()['user'],
158 6
            'to_user' => $toUser,
159 6
            'message' => $message,
160
        ]);
161
162 6
        return 'OK, I will tell <@' . $toUser . '> about your message';
163
    }
164
}
165