Completed
Push — develop ( 0a4bcc...0131cc )
by Kirill
25:10
created

Validator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 100
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validate() 0 24 5
A validateMessage() 0 16 4
A validateUser() 0 4 1
A validateTimeout() 0 5 1
A validateText() 0 7 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 App\Gitter\Middlewares\KarmaCounter;
12
13
use App\User;
14
use App\Message;
15
use Illuminate\Support\Str;
16
use Illuminate\Support\Collection;
17
18
/**
19
 * Class Validator
20
 * @package App\Gitter\Middlewares\KarmaCounter
21
 */
22
class Validator
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $likes = [];
28
29
    /**
30
     * Validator constructor.
31
     */
32
    public function __construct()
33
    {
34
        $this->likes = trans('karma.thanks');
0 ignored issues
show
Documentation Bug introduced by
It seems like trans('karma.thanks') 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...
35
    }
36
37
    /**
38
     * @param Message $message
39
     * @return Status[]|Collection
40
     */
41
    public function validate(Message $message)
42
    {
43
        $response = new Collection([]);
44
45
        // If has no mentions
46
        if (!count($message->mentions)) {
47
            if ($this->validateText($message)) {
48
                $response->push(new Status($message->user, Status::STATUS_NO_USER));
49
            }
50
51
            return $response;
52
        }
53
54
        foreach ($message->mentions as $mention) {
55
            // Ignore bot queries
56
            if (\Auth::user()->login === $message->user->login) {
57
                continue;
58
            }
59
60
            $response->push($this->validateMessage($message, $mention));
61
        }
62
63
        return $response;
64
    }
65
66
    /**
67
     * @param Message $message
68
     * @param User $mention
69
     * @return Status
70
     */
71
    protected function validateMessage(Message $message, User $mention)
72
    {
73
        if ($this->validateText($message)) {
74
            if (!$this->validateUser($message, $mention)) {
75
                return new Status($mention, Status::STATUS_SELF);
76
            }
77
78
            if (!$this->validateTimeout($message, $mention)) {
79
                return new Status($mention, Status::STATUS_TIMEOUT);
80
            }
81
82
            return new Status($mention, Status::STATUS_INCREMENT);
83
        }
84
85
        return new Status($mention, Status::STATUS_NOTHING);
86
    }
87
88
    /**
89
     * @param Message $message
90
     * @param User $mention
91
     * @return bool
92
     */
93
    protected function validateUser(Message $message, User $mention)
94
    {
95
        return $mention->login !== $message->user->login;
96
    }
97
98
    /**
99
     * @param Message $message
100
     * @param User $mention
101
     * @return bool
102
     */
103
    protected function validateTimeout(Message $message, User $mention)
104
    {
105
        return $mention->getLastKarmaTimeForRoom($message->room_id)->timestamp + 60
106
            < $message->created_at->timestamp;
107
    }
108
109
110
    /**
111
     * @param Message $message
112
     * @return bool
113
     */
114
    protected function validateText(Message $message)
115
    {
116
        $escapedText = $message->text->onlyWords()->getContent();
117
        return
118
            Str::endsWith($escapedText, $this->likes) ||
119
            Str::startsWith($escapedText, $this->likes);
120
    }
121
}
122