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

Validator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 99
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 6 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 = \Lang::get('thanks.likes');
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 (\Auth::user()->login === $message->user->login) {
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->getLastKarmaTimeForRoom($message->room_id)->timestamp + 60
105
            < $message->created_at->timestamp;
106
    }
107
108
109
    /**
110
     * @param Message $message
111
     * @return bool
112
     */
113
    protected function validateText(Message $message)
114
    {
115
        $escapedText = $message->text_without_special_chars;
116
117
        return Str::endsWith($escapedText, $this->likes) || Str::startsWith($escapedText, $this->likes);
118
    }
119
}
120