Completed
Push — master ( f848c8...48e11a )
by Kirill
03:23
created

PersonalAnswersMiddleware   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 8
Bugs 2 Features 3
Metric Value
c 8
b 2
f 3
dl 0
loc 126
rs 10
wmc 13
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D handle() 0 106 12
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 Interfaces\Gitter\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
    public function __construct()
25
    {
26
        $this->ai = new Ai();
27
    }
28
29
    /**
30
     * @param Message $message
31
     * @return mixed
32
     */
33
    public function handle(Message $message)
34
    {
35
        if ($message->user->login === \Auth::user()->login) {
36
            return $message;
37
        }
38
39
        $noMentions = !count($message->mentions);
40
41
        // Personal message
42
        $isBotMention = $message->hasMention(function(User $user) {
43
            return $user->login === \Auth::user()->login;
44
        });
45
46
        if ($isBotMention || $noMentions) {
47
            // Hello all
48
            $isHello = Str::contains($message->text_without_special_chars, \Lang::get('personal.hello_query'));
49
50
            if ($isHello) {
51
                $id = array_rand(\Lang::get('personal.hello'));
52
53
                $message->italic(\Lang::get('personal.hello.' . $id, [
54
                    'user' => $message->user->login
55
                ]));
56
            }
57
        }
58
59
        if (!count($message->mentions)) {
60
            // Question
61
            $isQuestion = Str::contains($message->text_without_special_chars, [
62
                'можно задать вопрос',
63
                'хочу задать вопрос',
64
                'есть кто',
65
                'кто может помочь',
66
                'помогите пожалуйста'
67
            ]);
68
69
            if ($isQuestion) {
70
                $message->italic(sprintf('@%s, и какой ответ ты ожидаешь услышать?', $message->user->login));
71
            }
72
73
            // Question
74
            $isCats = Str::contains($message->text_without_special_chars, ['котаны']);
75
76
            if ($isCats) {
77
                $message->italic(sprintf('@%s, в Пензу езжай со своими котанами \-_\-', $message->user->login));
78
            }
79
80
            // Question
81
            $isPolitics = Str::contains($message->text_without_special_chars, [
82
                'яровая',
83
                'пакет яровой',
84
                'пакетом яровой',
85
                'пакете яровой',
86
                'пакету яровой',
87
                'мизулина'
88
            ]);
89
90
            if ($isPolitics) {
91
                $message->italic(sprintf('@%s, :see_no_evil: :fire: ', $message->user->login));
92
            }
93
94
            $isRules = in_array($message->text_without_special_chars, [
95
                'правила чата',
96
                'правила',
97
                'как себя вести',
98
                '9 кругов'
99
            ], true);
100
101
            if ($isRules) {
102
                $message->italic(sprintf('@%s, [In rules we trust](http://laravel.su/articles/nine-circles-of-chat)', $message->user->login));
103
            }
104
105
            $isBan = in_array($message->text_without_special_chars, [
106
                'банхаммер',
107
                'хаммер',
108
                'бан',
109
            ], true);
110
111
            if ($isBan) {
112
                $message->italic(sprintf(
113
                    '@%s, тебе выданы ' . str_repeat(' :hammer: ', random_int(1, 9)) . ' на 0.' . random_int(1, 9) . ' секунды. Наслаждайся ;)',
114
                        $message->user->login
115
                ));
116
            }
117
118
            $isPolitics = in_array($message->text_without_special_chars, [
119
                'майдан',
120
                'революция',
121
                'битрикс',
122
                'yii',
123
                'wordpress',
124
                'вордпресс',
125
            ], true);
126
127
            if ($isPolitics) {
128
                $message->italic(sprintf(
129
                    '@%s, за родину! ' . str_repeat(' :monkey: ', random_int(1, 9)),
130
                    $message->user->login
131
                ));
132
            }
133
134
135
        }
136
137
        return $message;
138
    }
139
}
140