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

LongMessageMiddleware::handle()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 40
Code Lines 19

Duplication

Lines 20
Ratio 50 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 40
rs 8.439
cc 6
eloc 19
nc 4
nop 1
1
<?php
2
namespace Domains\Bot\Middlewares;
3
4
use Domains\Message;
5
use Interfaces\Gitter\Middleware\MiddlewareInterface;
6
7
/**
8
 * Class LongMessageMiddleware
9
 */
10
class LongMessageMiddleware implements MiddlewareInterface
11
{
12
    const MAX_CHARS = 1000;
13
    const MAX_LINES = 20;
14
    const MAX_CODE_LINES = 20;
15
16
    /**
17
     * @param Message $message
18
     * @return mixed
19
     */
20
    public function handle(Message $message)
21
    {
22
        $text = $message->text;
23
        $lines = count(explode("\n", $text));
24
        $chars = mb_strlen($text);
25
26
        if (preg_match_all('/^((`{3}\s*)(\w+)?(\s*([\w\W]+?)\n*)\2)\n*(?:[^\S\w\s]|$)/m', $text, $matches)) {
27
28
            $codeLines = 0;
29
30
            foreach ($matches[5] as $code) {
31
32
                $codeLines += count(explode("\n", $code));
33
34
            }
35
36 View Code Duplication
            if ($codeLines > self::MAX_CODE_LINES) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
38
                $answer = \Lang::get('long.code_personal', [
39
                    'user'  => $message->user->login,
40
                ]);
41
42
                $message->italic($answer);
43
44
                return null;
45
            }
46
47 View Code Duplication
        } elseif ($lines > self::MAX_LINES || $chars > self::MAX_CHARS) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
49
            $answer = \Lang::get('long.text_personal', [
50
                'user'  => $message->user->login,
51
            ]);
52
53
            $message->italic($answer);
54
55
            return null;
56
        }
57
58
        return $message;
59
    }
60
}
61