Validator::validateUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 09.10.2015 16:35
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Interfaces\Gitter\Karma;
12
13
use Domains\User;
14
use Domains\Message;
15
use Illuminate\Support\Collection;
16
use Illuminate\Support\Str;
17
18
/**
19
 * Class Validator
20
 */
21
class Validator
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $likes = [];
27
28
    /**
29
     * Validator constructor.
30
     */
31
    public function __construct()
32
    {
33
        $this->likes = trans('thanks.likes');
0 ignored issues
show
Documentation Bug introduced by
It seems like trans('thanks.likes') of type string is incompatible with the declared type array of property $likes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
    }
35
36
    /**
37
     * @param Message $message
38
     * @return Status[]|Collection
39
     */
40
    public function validate(Message $message)
41
    {
42
        $response = new Collection();
43
44
        // If has no mentions
45
        if (!count($message->mentions)) {
46
            if ($this->validateText($message)) {
47
                $response->push(new Status($message->user, Status::STATUS_NO_USER));
48
            }
49
50
            return $response;
51
        }
52
53
        foreach ($message->mentions as $mention) {
54
            // Ignore bot queries
55
            if ($message->user->isBot()) {
56
                continue;
57
            }
58
59
            $response->push($this->validateMessage($message, $mention));
60
        }
61
62
        return $response;
63
    }
64
65
    /**
66
     * @param Message $message
67
     * @param User $mention
68
     * @return Status
69
     */
70
    protected function validateMessage(Message $message, User $mention)
71
    {
72
        if ($this->validateText($message)) {
73
            if (!$this->validateUser($message, $mention)) {
74
                return new Status($mention, Status::STATUS_SELF);
75
            }
76
77
            if (!$this->validateTimeout($message, $mention)) {
78
                return new Status($mention, Status::STATUS_TIMEOUT);
79
            }
80
81
            return new Status($mention, Status::STATUS_INCREMENT);
82
        }
83
84
        return new Status($mention, Status::STATUS_NOTHING);
85
    }
86
87
    /**
88
     * @param Message $message
89
     * @param User $mention
90
     * @return bool
91
     */
92
    protected function validateUser(Message $message, User $mention)
93
    {
94
        return $mention->login !== $message->user->login;
95
    }
96
97
    /**
98
     * @param Message $message
99
     * @param User $mention
100
     * @return bool
101
     */
102
    protected function validateTimeout(Message $message, User $mention)
103
    {
104
        return $mention
105
            ->getLastKarmaTimeForRoom($message->room_id)
106
            ->addSeconds(60)
107
            ->lt($message->created_at);
108
    }
109
110
111
    /**
112
     * @param Message $message
113
     * @return bool
114
     */
115
    protected function validateText(Message $message)
116
    {
117
        $escapedText = $message->text_without_special_chars;
118
119
        return Str::endsWith($escapedText, $this->likes) || Str::startsWith($escapedText, $this->likes);
120
    }
121
}
122