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

Message   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 5
cbo 2
dl 0
loc 136
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getUpdatedAtAttribute() 0 8 2
A hasMention() 0 9 3
A getEscapedTextAttribute() 0 9 1
A getTextWithoutSpecialCharsAttribute() 0 9 1
A answer() 0 13 2
A pre() 0 4 1
A code() 0 6 1
A italic() 0 4 1
A bold() 0 4 1
A decorate() 0 13 2
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 09.10.2015 16:58
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 Domains;
12
13
use Carbon\Carbon;
14
use LogicException;
15
use Interfaces\Gitter\Client;
16
use Core\Mappers\MessageMapperTrait;
17
use Illuminate\Database\Eloquent\Model;
18
19
/**
20
 * Class Message
21
 *
22
 * @property string $gitter_id
23
 * @property string $text
24
 * @property string $html
25
 * @property bool $edited
26
 * @property User $user
27
 * @property string $unread
28
 * @property int $read_by
29
 * @property array $urls
30
 * @property User[] $mentions
31
 * @property array $issues
32
 * @property array $meta
33
 * @property string $room_id
34
 * @property Carbon $created_at
35
 * @property Carbon $updated_at
36
 *
37
 * @property-read string $escaped_text
38
 * @property-read string $text_without_special_chars
39
 *
40
 */
41
class Message extends Model
42
{
43
    /**
44
     * Gitter mapper
45
     */
46
    use MessageMapperTrait;
47
48
    /**
49
     * @param $value
50
     * @return Carbon
51
     */
52
    public function getUpdatedAtAttribute($value)
53
    {
54
        if ($value === null) {
55
            return $this->created_at;
56
        }
57
58
        return $value;
59
    }
60
61
    /**
62
     * @param callable $cb
63
     * @return bool
64
     */
65
    public function hasMention(callable $cb)
66
    {
67
        foreach ($this->mentions as $mention) {
68
            if ($cb($mention)) {
69
                return true;
70
            }
71
        }
72
        return false;
73
    }
74
75
    /**
76
     * @return mixed|string
77
     */
78
    public function getEscapedTextAttribute()
79
    {
80
        $text = $this->text;
81
        $text = mb_strtolower($text);
82
        $text = str_replace(["\n", "\r"], ' ', $text);
83
        $text = trim($text);
84
85
        return $text;
86
    }
87
88
    /**
89
     * @return mixed|string
90
     */
91
    public function getTextWithoutSpecialCharsAttribute()
92
    {
93
        $escapedText = $this->escaped_text;
94
        $escapedText = preg_replace('/\@[a-z0-9\-_]+/iu', '', $escapedText);
95
        $escapedText = preg_replace('/[^\s\w]/iu', '', $escapedText);
96
        $escapedText = trim($escapedText);
97
98
        return $escapedText;
99
    }
100
101
    /**
102
     * @param $text
103
     * @return $this
104
     */
105
    public function answer($text)
106
    {
107
        if (\Config::get('gitter.output')) {
108
            $client = \App::make(Client::class);
109
            $room = \App::make(Room::class);
110
111
            $client->request('message.send', ['roomId' => $room->id], [
112
                'text' => (string)$text,
113
            ], 'POST');
114
        }
115
116
        return $this;
117
    }
118
119
    /**
120
     * @param $text
121
     * @return Model
122
     */
123
    public function pre($text)
124
    {
125
        return $this->answer($this->decorate('`', $text));
126
    }
127
128
    /**
129
     * @param $code
130
     * @param string $lang
131
     * @return Model
132
     */
133
    public function code($code, $lang = '')
134
    {
135
        return $this->answer(
136
            '```' . $lang . "\n" . $code . "\n" . '```'
137
        );
138
    }
139
140
    /**
141
     * @param $text
142
     * @return Model
143
     */
144
    public function italic($text)
145
    {
146
        return $this->answer($this->decorate('_', $text));
147
    }
148
149
    /**
150
     * @param $text
151
     * @return Model
152
     */
153
    public function bold($text)
154
    {
155
        return $this->answer($this->decorate('**', $text));
156
    }
157
158
    /**
159
     * @param $symbol
160
     * @param $text
161
     * @return string
162
     */
163
    protected function decorate($symbol, $text)
164
    {
165
        $result = [];
166
        $strings = explode("\n", $text);
167
        foreach ($strings as $string) {
168
            $result[] =
169
                $symbol .
170
                str_replace($symbol, '\\' . $symbol, trim($string)) .
171
                $symbol;
172
        }
173
174
        return implode("\n", $result);
175
    }
176
}
177