CommentPresenter::markdownBody()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Usamamuneerchaudhary\Commentify\Models\Presenters;
4
5
use Illuminate\Support\HtmlString;
6
use Usamamuneerchaudhary\Commentify\Models\Comment;
7
use Usamamuneerchaudhary\Commentify\Models\User;
8
9
class CommentPresenter
10
{
11
    /**
12
     * @var Comment
13
     */
14
    public Comment $comment;
15
16
    /**
17
     * @param  Comment  $comment
18
     */
19
    public function __construct(Comment $comment)
20
    {
21
        $this->comment = $comment;
22
    }
23
24
    /**
25
     * @return HtmlString
26
     */
27
    public function markdownBody(): HtmlString
28
    {
29
        return new HtmlString(app('markdown')->convertToHtml($this->comment->body));
0 ignored issues
show
Deprecated Code introduced by
The function League\CommonMark\Markdo...verter::convertToHtml() has been deprecated: since 2.2; use {@link convert()} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

29
        return new HtmlString(/** @scrutinizer ignore-deprecated */ app('markdown')->convertToHtml($this->comment->body));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
30
    }
31
32
    /**
33
     * @return mixed
34
     */
35
    public function relativeCreatedAt(): mixed
36
    {
37
        return $this->comment->created_at->diffForHumans();
38
    }
39
40
    /**
41
     * @param $text
42
     * @return array|string
43
     */
44
    public function replaceUserMentions($text): array|string
45
    {
46
        preg_match_all('/@([A-Za-z0-9_]+)/', $text, $matches);
47
        $usernames = $matches[1];
48
        $replacements = [];
49
50
        foreach ($usernames as $username) {
51
            $user = User::where('name', $username)->first();
52
53
            if ($user) {
54
                $userRoutePrefix = config('commentify.users_route_prefix', 'users');
55
56
                $replacements['@'.$username] = '<a href="/'.$userRoutePrefix.'/'.$username.'">@'.$username.
57
                    '</a>';
58
            } else {
59
                $replacements['@'.$username] = '@'.$username;
60
            }
61
        }
62
63
        return str_replace(array_keys($replacements), array_values($replacements), $text);
64
    }
65
66
67
}
68