Completed
Pull Request — master (#41)
by
unknown
05:42
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 1
Metric Value
c 1
b 0
f 1
dl 20
loc 40
rs 8.439
cc 6
eloc 19
nc 4
nop 1
1
<?php
2
namespace App\Middlewares;
3
4
use App\Message;
5
use App\Gitter\Middleware\MiddlewareInterface;
6
7
/**
8
 * Class LongMessageMiddleware
9
 * @package App\Gitter\Middleware
10
 */
11
class LongMessageMiddleware implements MiddlewareInterface
12
{
13
    const MAX_CHARS = 500;
14
    const MAX_LINES = 15;
15
    const MAX_CODE_LINES = 20;
16
17
    /**
18
     * @param Message $message
19
     * @return mixed
20
     */
21
    public function handle(Message $message)
22
    {
23
        $text = $message->escaped_text;
24
        $lines = count(explode("\n", $text));
25
        $chars = mb_strlen($text);
26
27
        if (preg_match_all('/^((`{3}\s*)(\w+)?(\s*([\w\W]+?)\n*)\2)\n*(?:[^\S\w\s]|$)/m', $text, $matches)) {
28
29
            $codeLines = 0;
30
31
            foreach ($matches[5] as $code) {
32
33
                $codeLines += count(explode("\n", $code));
34
35
            }
36
37 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...
38
39
                $answer = \Lang::get('long.code_personal', [
40
                    'user'  => $message->user->login,
41
                ]);
42
43
                $message->italic($answer);
44
45
                return null;
46
            }
47
48 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...
49
50
            $answer = \Lang::get('long.text_personal', [
51
                'user'  => $message->user->login,
52
            ]);
53
54
            $message->italic($answer);
55
56
            return null;
57
        }
58
59
        return $message;
60
    }
61
}
62