Completed
Push — develop ( 9773af...b9ad6f )
by Kirill
32:11 queued 50s
created

GoogleSearchMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 64
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 23 5
A getUserLogin() 0 14 3
1
<?php
2
namespace App\Gitter\Middlewares;
3
4
use App\Room;
5
use App\Message;
6
use App\Gitter\Extensions\Middleware\MiddlewareInterface;
7
use Gitter\Client;
8
9
/**
10
 * Class GoogleSearchMiddleware
11
 * @package App\Bot\Middlewares
12
 */
13
class GoogleSearchMiddleware implements MiddlewareInterface
14
{
15
    /**
16
     * @var Room
17
     */
18
    protected $room;
19
20
    /**
21
     * PingPongMiddleware constructor.
22
     * @param Room $room
23
     */
24
    public function __construct(Room $room)
25
    {
26
        $this->room = $room;
27
    }
28
29
    /**
30
     * @param Message $message
31
     * @param \Closure $next
32
     * @return string|null
33
     */
34
    public function handle(Message $message, \Closure $next)
35
    {
36
        $matches = $message->text->escape()->matches('(@.*?\s)?(?:погугли|гугли)\s(.*?)');
37
38
        if ($matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39
            if (!trim($matches[2])) {
40
                return $next($message);
41
            }
42
43
            $mention = $this->getUserLogin($message);
44
45
            return trim($matches[1]) && $mention
46
                ? trans('google.personal', [
47
                    'user'  => $mention->login,
48
                    'query' => urlencode($matches[2]),
49
                ])
50
                : trans('google.common', [
51
                    'query' => urlencode($matches[2]),
52
                ]);
53
        }
54
55
        return $next($message);
56
    }
57
58
    /**
59
     * @param Message $message
60
     * @return \App\User|null
61
     */
62
    protected function getUserLogin(Message $message)
63
    {
64
        $hasMentions = $message->mentions->count();
65
        $mention = null;
66
67
        if ($hasMentions) {
68
            $first = $message->mentions[0]->user;
69
            $mention = $first->login === \Auth::user()->login
70
                ? $message->user
71
                : $first;
72
        }
73
74
        return $mention;
75
    }
76
}
77
78