Code Duplication    Length = 69-71 lines in 2 locations

frontend/widgets/comment/MediaComment.php 1 location

@@ 19-89 (lines=71) @@
16
 * @author Agiel K. Saputra <[email protected]>
17
 * @since 0.1.0
18
 */
19
class MediaComment extends BaseComment
20
{
21
    /**
22
     * Set comment and pagination.
23
     * Select all comments from database and create pagination.
24
     * Get child of current comment.
25
     */
26
    protected function setComments()
27
    {
28
        /* @var $models \common\models\BaseComment */
29
        $comments = [];
30
        $query = Comment::find()
31
            ->select(['id', 'author', 'email', 'url', 'date', 'content'])
32
            ->andWhere(['parent' => 0, 'media_id' => $this->model->id, 'status' => 'approved'])
33
            ->andWhere(['<=', 'date', date('Y-m-d H:i:s')])
34
            ->orderBy(['date' => $this->commentOrder]);
35
36
        $countQuery = clone $query;
37
38
        $pages = new Pagination([
39
            'totalCount' => $countQuery->count(),
40
            'pageSize' => $this->pageSize,
41
        ]);
42
43
        $this->pages = $pages;
44
45
        $models = $query
46
            ->offset($pages->offset)
47
            ->limit($pages->limit)
48
            ->all();
49
50
        /* @var $model \common\models\PostComment */
51
        foreach ($models as $model) {
52
            $comments[$model->id] = $model;
53
            $comments[$model->id]['child'] = $this->getChildren($model->id);
54
        }
55
56
        $this->comments = $comments;
57
    }
58
59
60
    /**
61
     * Get comment children based on comment ID.
62
     *
63
     * @param int $id
64
     * @return array|null
65
     */
66
    protected function getChildren($id)
67
    {
68
        $comments = [];
69
70
        $models = Comment::find()
71
            ->select(['id', 'author', 'email', 'url', 'date', 'content'])
72
            ->andWhere(['parent' => $id, 'media_id' => $this->model->id, 'status' => 'approved'])
73
            ->andWhere(['<=', 'date', date('Y-m-d H:i:s')])
74
            ->orderBy(['date' => $this->commentOrder])
75
            ->all();
76
77
        if (empty($models)) {
78
            $comments = null;
79
        } else {
80
            /* @var $model \common\models\PostComment */
81
            foreach ($models as $model) {
82
                $comments[$model->id] = $model;
83
                $comments[$model->id]['child'] = $this->getChildren($model->id);
84
            }
85
        }
86
87
        return $comments;
88
    }
89
}
90

frontend/widgets/comment/PostComment.php 1 location

@@ 19-87 (lines=69) @@
16
 * @author Agiel K. Saputra <[email protected]>
17
 * @since 0.1.0
18
 */
19
class PostComment extends BaseComment
20
{
21
    /**
22
     * Set comment and pagination.
23
     * Select all comments from database and create pagination.
24
     * Get child of current comment.
25
     */
26
    protected function setComments()
27
    {
28
        /* @var $models \common\models\BaseComment */
29
        $comments = [];
30
31
        $query = Comment::find()
32
            ->select(['id', 'author', 'email', 'url', 'date', 'content'])
33
            ->andWhere(['parent' => 0, 'post_id' => $this->model->id, 'status' => 'approved'])
34
            ->andWhere(['<=', 'date', date('Y-m-d H:i:s')])
35
            ->orderBy(['id' => $this->commentOrder]);
36
37
        $countQuery = clone $query;
38
39
        $pages = new Pagination([
40
            'totalCount' => $countQuery->count(),
41
            'pageSize' => $this->pageSize,
42
        ]);
43
44
        $this->pages = $pages;
45
46
        $models = $query
47
            ->offset($pages->offset)
48
            ->limit($pages->limit)
49
            ->all();
50
51
        foreach ($models as $model) {
52
            $comments[$model->id] = $model;
53
            $comments[$model->id]['child'] = $this->getChildren($model->id);
54
        }
55
56
        $this->comments = $comments;
57
    }
58
59
    /**
60
     * Get comment children based on comment ID.
61
     *
62
     * @param int $id
63
     * @return array|null
64
     */
65
    protected function getChildren($id)
66
    {
67
        /* @var $models \common\models\PostComment[] */
68
        $comments = [];
69
        $models = Comment::find()
70
            ->select(['id', 'author', 'email', 'url', 'date', 'content'])
71
            ->andWhere(['parent' => $id, 'post_id' => $this->model->id, 'status' => 'approved'])
72
            ->andWhere(['<=', 'date', date('Y-m-d H:i:s')])
73
            ->orderBy(['id' => $this->commentOrder])
74
            ->all();
75
76
        if (empty($models)) {
77
            $comments = null;
78
        } else {
79
            foreach ($models as $model) {
80
                $comments[$model->id] = $model;
81
                $comments[$model->id]['child'] = $this->getChildren($model->id);
82
            }
83
        }
84
85
        return $comments;
86
    }
87
}
88