Code Duplication    Length = 26-27 lines in 2 locations

Apps/Controller/Api/Comments.php 2 locations

@@ 46-72 (lines=27) @@
43
44
        // build output json data as array
45
        $data = [];
46
        foreach ($records as $comment) {
47
            // comment can be passed from registered user (with unique ID) or from guest (without ID)
48
            $userName = __('Unknown');
49
            $userAvatar = App::$Alias->scriptUrl . '/upload/user/avatar/small/default.jpg';
50
            $userObject = $comment->getUser();
51
            if ($userObject !== null) {
52
                $userName = $userObject->getProfile()->nick;
53
                $userAvatar = $userObject->getProfile()->getAvatarUrl('small');
54
            } else {
55
                if (!Str::likeEmpty($comment->guest_name)) {
56
                    $userName = App::$Security->strip_tags($comment->guest_name);
57
                }
58
            }
59
60
            // build output json data
61
            $data[] = [
62
                'id' => $comment->id,
63
                'text' => $comment->message,
64
                'date' => Date::convertToDatetime($comment->created_at, Date::FORMAT_TO_HOUR),
65
                'user' => [
66
                    'id' => $comment->user_id,
67
                    'name' => $userName,
68
                    'avatar' => $userAvatar
69
                ],
70
                'answers' => $comment->getAnswerCount()
71
            ];
72
        }
73
74
        // calculate comments left count
75
        $count = CommentPost::where('pathway', '=', $path)->count();
@@ 103-128 (lines=26) @@
100
101
        // prepare output
102
        $response = [];
103
        foreach ($records->get() as $row) {
104
            $userInstance = $row->getUser();
105
            $userName = __('Unknown');
106
            $userAvatar = App::$Alias->scriptUrl . '/upload/user/avatar/small/default.jpg';
107
            if ($userInstance !== null) {
108
                if (!Str::likeEmpty($userInstance->getProfile()->nick)) {
109
                    $userName = $userInstance->getProfile()->nick;
110
                }
111
                $userAvatar = $userInstance->getProfile()->getAvatarUrl('small');
112
            } else {
113
                if (!Str::likeEmpty($row->guest_name)) {
114
                    $userName = App::$Security->strip_tags($row->guest_name);
115
                }
116
            }
117
118
            $response[] = [
119
                'id' => $row->id,
120
                'text' => $row->message,
121
                'date' => Date::convertToDatetime($row->created_at, Date::FORMAT_TO_HOUR),
122
                'user' => [
123
                    'id' => $row->user_id,
124
                    'name' => $userName,
125
                    'avatar' => $userAvatar
126
                ]
127
            ];
128
        }
129
130
        return json_encode([
131
            'status' => 1,