PersonalAnswersMiddleware::handle()   F
last analyzed

Complexity

Conditions 13
Paths 388

Size

Total Lines 111
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 3 Features 4
Metric Value
cc 13
eloc 69
c 13
b 3
f 4
nc 388
nop 1
dl 0
loc 111
rs 3.7737

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Domains\Bot\Middlewares;
3
4
use Interfaces\Gitter\Ai;
5
use Domains\Message;
6
use Interfaces\Gitter\Karma\Validator;
7
use Domains\Middleware\MiddlewareInterface;
8
use Domains\User;
9
use Illuminate\Support\Str;
10
11
/**
12
 * Class PersonalAnswersMiddleware
13
 */
14
class PersonalAnswersMiddleware implements MiddlewareInterface
15
{
16
    /**
17
     * @var Ai
18
     */
19
    protected $ai;
20
21
    /**
22
     * PersonalAnswersMiddleware constructor.
23
     *
24
     * @param Ai $ai
25
     */
26
    public function __construct(Ai $ai)
27
    {
28
        $this->ai = $ai;
29
    }
30
31
    /**
32
     * @param Message $message
33
     * @return mixed
34
     */
35
    public function handle(Message $message)
36
    {
37
        if ($message->user->isBot()) {
38
            return $message;
39
        }
40
41
        $noMentions = !count($message->mentions);
42
43
        // Personal message
44
        $isBotMention = $message->hasMention(function(User $user) {
45
            return $user->isBot();
46
        });
47
48
        if ($isBotMention || $noMentions) {
49
            // Hello all
50
            $isHello = Str::contains($message->text_without_special_chars, trans('personal.hello_query'));
51
52
            if ($isHello) {
53
                $id = array_rand(trans('personal.hello'));
54
55
                $message->italic(trans('personal.hello.' . $id, [
56
                    'user' => $message->user->login
57
                ]));
58
            }
59
        }
60
61
        if (!count($message->mentions)) {
62
            // Question
63
            $isQuestion = in_array($message->text_without_special_chars, [
64
                'можно задать вопрос',
65
                'хочу задать вопрос',
66
                'есть кто',
67
                'кто может помочь',
68
                'помогите пожалуйста'
69
            ], true);
70
71
            if ($isQuestion) {
72
                $message->italic(sprintf('@%s, и какой ответ ты ожидаешь услышать?', $message->user->login));
73
            }
74
75
            // Question
76
            $isCats = Str::contains($message->text_without_special_chars, ['котаны']);
77
78
            if ($isCats) {
79
                $message->italic(sprintf('@%s, в Пензу езжай со своими котанами \-_\-', $message->user->login));
80
            }
81
82
            // Question
83
            $isPolitics = Str::contains($message->text_without_special_chars, [
84
                'яровая',
85
                'пакет яровой',
86
                'пакетом яровой',
87
                'пакете яровой',
88
                'пакету яровой',
89
                'мизулина'
90
            ]);
91
92
            if ($isPolitics) {
93
                $message->italic(sprintf('@%s, :see_no_evil: :fire: ', $message->user->login));
94
            }
95
96
            $isRules = in_array($message->text_without_special_chars, [
97
                'правила чата',
98
                'правила',
99
                'как себя вести',
100
                '9 кругов'
101
            ], true);
102
103
            if ($isRules) {
104
                $message->italic(sprintf('@%s, [url=http://laravel.su/articles/nine-circles-of-chat]In rules we trust[/url]', $message->user->login));
105
            }
106
107
            $isBan = in_array($message->text_without_special_chars, [
108
                'банхаммер',
109
                'хаммер',
110
                'бан',
111
            ], true);
112
113
            if ($isBan) {
114
                $message->italic(sprintf(
115
                    '@%s, тебе выданы ' . str_repeat(' :hammer: ', random_int(1, 9)) . ' на 0.' . random_int(1, 9) . ' секунды. Наслаждайся ;)',
116
                        $message->user->login
117
                ));
118
            }
119
120
            $isPolitics = in_array($message->text_without_special_chars, [
121
                'майдан',
122
                'революция',
123
                'битрикс',
124
                'yii',
125
                'wordpress',
126
                'вордпресс',
127
                'laravel',
128
                'ларавель',
129
                'йии',
130
            ], true);
131
132
            if ($isPolitics) {
133
                $message->italic(sprintf(
134
                    '@%s, за ' . $message->text_without_special_chars . '! ' . str_repeat(' :monkey: ', random_int(1, 9)),
135
                    $message->user->login
136
                ));
137
            }
138
139
            if (preg_match('/^[a-zA-Z][0-9]$/isu', $message->text_without_special_chars)) {
140
                $message->italic(sprintf('@%s, %s', $message->user->login, ['мимо', 'ранил', 'убил'][random_int(0, 2)]));
141
            }
142
        }
143
144
        return $message;
145
    }
146
}
147