Completed
Push — master ( 191869...afc708 )
by Frank
08:08
created

TellCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 11
loc 11
rs 9.4285
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 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
        $this->commandName = 'tell';
35
        $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
        parent::__construct($payload, $client);
42
    }
43
44
    /**
45
     * @return bool|string
46
     *
47
     * @throws \Doctrine\DBAL\DBALException
48
     */
49
    public function process()
50
    {
51
        $message = $this->payload->getData()['text'];
52
        $this->params = array_map('trim', explode(' ', $message));
53
        $result = false;
54
        if ($this->params[0] === 'tell') {
55
            $result = $this->processTell();
56
        }
57
58
        return $result;
59
    }
60
61
    /**
62
     * @param string $user
63
     * @param string $presence
64
     *
65
     * @throws \Doctrine\DBAL\DBALException
66
     */
67
    public function processPresenceChange($user, $presence)
68
    {
69
        if ($presence === self::PRESENCE_ACTIVE) {
70
            $queryBuilder = $this->getDatabaseConnection()->createQueryBuilder();
71
            $notifications = $queryBuilder
72
                ->select('*')
73
                ->from('notifications')
74
                ->where($queryBuilder->expr()->eq('to_user', $queryBuilder->createNamedParameter($user)))
75
                ->andWhere($queryBuilder->expr()->eq('delivered', $queryBuilder->createNamedParameter('0000-00-00 00:00:00')))
76
                ->execute()
77
                ->fetchAll();
78
            foreach ($notifications as $notification) {
79
                if (strpos($notification['message'], 'review:') === 0) {
80
                    $parts = explode(':', $notification['message']);
81
                    $refId = (int) trim($parts[1]);
82
                    $result = $this->queryGerrit('change:' . $refId);
83
                    $msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>'
84
                        . ' ask you to look at this patch:*';
85
86
                    if (is_array($result)) {
87
                        foreach ($result as $item) {
88
                            if ((int) $item->_number === $refId) {
89
                                $message = $this->buildReviewMessage($item);
90
                                $message->setText($msg);
91
                                $this->sendResponse($message, $user);
92
                            }
93
                        }
94
                    }
95
                } elseif (strpos($notification['message'], 'forge:') === 0) {
96
                    $parts = explode(':', $notification['message']);
97
                    $issueNumber = (int) trim($parts[1]);
98
                    $result = $this->queryForge('issues/' . $issueNumber);
99 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...
100
                        $msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>'
101
                            . ' ask you to look at this issue:*';
102
                        $this->sendResponse($msg . "\n" . $this->buildIssueMessage($result->issue), $user);
103
                    }
104 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...
105
                    $msg = '*Hi <@' . $user . '>, here is a message from <@' . $notification['from_user'] . '>'
106
                        . ' for you:*';
107
                    $this->sendResponse($msg . "\n" . $notification['message'], $user);
108
                }
109
                $now = new \DateTime();
110
                $now->setTimestamp(time());
111
                $this->getDatabaseConnection()->update(
112
                    'notifications',
113
                    ['delivered' => $now],
114
                    ['id' => $notification['id']],
115
                    ['datetime']
116
                );
117
            }
118
        }
119
    }
120
121
    /**
122
     * @return string
123
     *
124
     * @throws \Doctrine\DBAL\DBALException
125
     */
126
    protected function processTell() : string
127
    {
128
        $params = $this->params;
129
        array_shift($params);
130
        $toUser = array_shift($params);
131
        $toUser = str_replace(['<', '>', '@'], '', $toUser);
132
        if ($params[0] === 'about'
133
            && (strpos($params[1], 'review:') !== false || strpos($params[1], 'forge:') !== false)) {
134
            $message = $params[1];
135
        } else {
136
            $message = implode(' ', $params);
137
        }
138
        $this->getDatabaseConnection()->insert('notifications', [
139
            'from_user' => $this->payload->getData()['user'],
140
            'to_user' => $toUser,
141
            'message' => $message,
142
        ]);
143
144
        return 'OK, I will tell <@' . $toUser . '> about your message';
145
    }
146
}
147