Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

PersonalAnswersMiddleware::handle()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 43
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 8.439
cc 5
eloc 18
nc 6
nop 1
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
        // Personal message
40
        $isBotMention = $message->hasMention(function(User $user) {
41
            return $user->login === \Auth::user()->login;
42
        });
43
44
        if ($isBotMention) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
45
            //$this->ai->handle($message);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
47
48
        } else {
49
50
            // Hello all
51
            $isHello = Str::contains($message->text_without_special_chars, \Lang::get('personal.hello_query'));
52
53
            if ($isHello) {
54
                $id = array_rand(\Lang::get('personal.hello'));
55
56
                $message->italic(\Lang::get('personal.hello.' . $id, [
57
                    'user' => $message->user->login
58
                ]));
59
            }
60
61
62
            // Question
63
            $isQuestion = Str::contains($message->text_without_special_chars, [
64
                'можно задать вопрос',
65
                'хочу задать вопрос'
66
            ]);
67
68
            if ($isQuestion) {
69
                $message->italic(sprintf('@%s, и какой ответ ты ожидаешь услышать?', $message->user->login));
70
            }
71
72
        }
73
74
        return $message;
75
    }
76
}
77