Passed
Push — master ( 42e076...8f5376 )
by Mihail
17:56
created

Comments::actionList()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 68
Code Lines 42

Duplication

Lines 27
Ratio 39.71 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 27
loc 68
rs 6.9654
cc 7
eloc 42
nc 10
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Apps\Controller\Api;
4
5
use Apps\ActiveRecord\CommentPost;
6
use Apps\ActiveRecord\CommentAnswer;
7
use Apps\ActiveRecord\App as AppRecord;
8
use Extend\Core\Arch\ApiController;
9
use Ffcms\Core\App;
10
use Ffcms\Core\Exception\JsonException;
11
use Ffcms\Core\Helper\Date;
12
use Ffcms\Core\Helper\Type\Obj;
13
use Ffcms\Core\Helper\Type\Str;
14
15
class Comments extends ApiController
16
{
17
    const ITEM_PER_PAGE = 10;
18
19
    public function actionList($index)
20
    {
21
        // set header
22
        $this->setJsonHeader();
23
        // get config count per page
24
        $perPage = (int)AppRecord::getConfig('widget', 'Comments', 'perPage');
25
        // offset can be only integer
26
        $index = (int)$index;
27
        $offset = $perPage * $index;
28
        // get comment target path and check
29
        $path = (string)App::$Request->query->get('path');
30
        if (Str::likeEmpty($path)) {
31
            throw new JsonException('Wrong path');
32
        }
33
34
        // select comments from db and check it
35
        $records = CommentPost::where('pathway', '=', $path)
36
            ->skip($offset)
37
            ->take($perPage)
38
            ->get();
39
40
        if ($records->count() < 1) {
41
            throw new JsonException('No comments is found');
42
        }
43
44
        // build output json data as array
45
        $data = [];
46 View Code Duplication
        foreach ($records as $comment) {
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...
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();
76
        $count -= $offset + $perPage;
77
        if ($count < 0) {
78
            $count = 0;
79
        }
80
81
        return json_encode([
82
            'status' => 1,
83
            'data' => $data,
84
            'leftCount' => $count
85
        ]);
86
    }
87
88
    public function actionShowanswers($commentId)
89
    {
90
        // check input data
91
        if (!Obj::isLikeInt($commentId) || (int)$commentId < 1) {
92
            throw new JsonException('Input data is incorrect');
93
        }
94
95
        // get data from db by comment id
96
        $records = CommentAnswer::where('comment_id', '=', $commentId);
97
        if ($records->count() < 1) {
98
            throw new JsonException('No answers for comment is founded');
99
        }
100
101
        // prepare output
102
        $response = [];
103 View Code Duplication
        foreach ($records->get() as $row) {
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...
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,
132
            'data' => $response
133
        ]);
134
    }
135
136
}