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

BeerCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 12
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 12
loc 12
rs 9.4285
c 2
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 BeerCommand.
17
 *
18
 * @property string commandName
19
 * @property array helpCommands
20
 */
21
class BeerCommand extends AbstractCommand
22
{
23
    /**
24
     * AbstractCommand constructor.
25
     *
26
     * @param Payload        $payload
27
     * @param RealTimeClient $client
28
     */
29 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...
30
    {
31
        $this->commandName = 'beer';
32
        $this->helpCommands = [
33
            'help' => 'shows this help',
34
            'stats [username]' => 'show beer counter for [username]',
35
            'for [username]' => 'give [username] a T3Beer',
36
            'all' => 'show all beer counter',
37
            'top10' => 'show TOP 10',
38
        ];
39
        parent::__construct($payload, $client);
40
    }
41
42
    /**
43
     * stats for all beer counter.
44
     *
45
     * @return string
46
     *
47
     * @throws \Doctrine\DBAL\DBALException
48
     */
49
    protected function processAll() : string
50
    {
51
        return 'Yeah, ' . $this->getBeerCountAll() . ' :t3beer: spend to all people';
52
    }
53
54
    /**
55
     * stats for TOP 10.
56
     *
57
     * @return string
58
     *
59
     * @throws \Doctrine\DBAL\DBALException
60
     */
61
    protected function processTop10() : string
62
    {
63
        $rows = $this->getBeerTop10();
64
        $text = ['*Yeah, here are the TOP 10*'];
65
        foreach ($rows as $row) {
66
            $text[] = '<@' . $row['username'] . '> has received ' . $row['cnt'] . ' :t3beer:';
67
        }
68
69
        return implode(chr(10), $text);
70
    }
71
72
    /**
73
     * stats for beer counter.
74
     *
75
     * @return string
76
     *
77
     * @throws \Doctrine\DBAL\DBALException
78
     */
79
    protected function processStats() : string
80
    {
81
        $params = $this->params;
82
        array_shift($params);
83
        $username = trim($params[0]);
84
        if (strpos($username, '<') === 0 && $username[1] === '@') {
85
            $username = str_replace(['<', '>', '@'], '', $username);
86
87
            return '<@' . $username . '> has received ' . $this->getBeerCountByUsername($username) . ' :t3beer: so far';
88
        }
89
        return '*Sorry, a username must start with a @-sign:*';
90
    }
91
92
    /**
93
     * give someone a beer.
94
     *
95
     * @return string
96
     *
97
     * @throws \Doctrine\DBAL\DBALException
98
     */
99
    protected function processFor() : string
100
    {
101
        $from_user = $this->payload->getData()['user'];
102
        $params = $this->params;
103
        array_shift($params);
104
        $username = trim($params[0]);
105
        if (strpos($username, '<') === 0 && $username[1] === '@') {
106
            $username = str_replace(['<', '>', '@'], '', $username);
107
            $queryBuilder = $this->getDatabaseConnection()
108
                ->createQueryBuilder();
109
            $record = $queryBuilder
110
                ->select('tstamp')
111
                ->from('beers')
112
                ->where($queryBuilder->expr()->eq('to_user', $queryBuilder->createNamedParameter($username)))
113
                ->andWhere($queryBuilder->expr()->eq('from_user', $queryBuilder->createNamedParameter($from_user)))
114
                ->orderBy('tstamp', 'DESC')
115
                ->setMaxResults(1)
116
                ->execute()
117
                ->fetch();
118
            $addBeer = false;
119
            if (empty($record)) {
120
                $addBeer = true;
121
            } elseif ($record['tstamp'] + 86400 < time()) {
122
                $addBeer = true;
123
            }
124
            if ($addBeer) {
125
                $data = [
126
                    'to_user' => $username,
127
                    'from_user' => $from_user,
128
                    'tstamp' => time()
129
                ];
130
                $this->getDatabaseConnection()->insert('beers', $data);
131
                return 'Yeah, one more :t3beer: for <@' . $username . '>' . chr(10) . '<@' . $username . '> has received '
132
                    . $this->getBeerCountByUsername($username) . ' :t3beer: so far';
133
            }
134
            return 'You spend one :t3beer: to <@' . $username . '> within in last 24 hours. Too much beer is unhealthy ;)';
135
        }
136
        return '*Sorry, a username must start with a @-sign:*';
137
    }
138
139
    /**
140
     * @param $username
141
     *
142
     * @return int
143
     *
144
     * @throws \Doctrine\DBAL\DBALException
145
     */
146
    protected function getBeerCountByUsername($username) : int
147
    {
148
        $queryBuilder = $this->getDatabaseConnection()
149
            ->createQueryBuilder();
150
        return $queryBuilder
151
            ->select('*')
152
            ->from('beers')
153
            ->where($queryBuilder->expr()->eq('to_user', $queryBuilder->createNamedParameter($username)))
154
            ->execute()
155
            ->rowCount();
156
    }
157
158
    /**
159
     * @return int
160
     *
161
     * @throws \Doctrine\DBAL\DBALException
162
     */
163
    protected function getBeerCountAll() : int
164
    {
165
        return $this->getDatabaseConnection()
166
            ->createQueryBuilder()
167
            ->select('*')
168
            ->from('beers')
169
            ->execute()
170
            ->rowCount();
171
    }
172
173
    /**
174
     * @return array
175
     *
176
     * @throws \Doctrine\DBAL\DBALException
177
     */
178
    protected function getBeerTop10() : array
179
    {
180
        return $this->getDatabaseConnection()
181
            ->createQueryBuilder()
182
            ->select('count(*) AS cnt', 'to_user AS username')
183
            ->from('beers')
184
            ->groupBy('to_user')
185
            ->orderBy('cnt', 'DESC')
186
            ->setMaxResults(10)
187
            ->execute()
188
            ->fetchAll();
189
    }
190
}
191