CommentListWidget   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 12
c 6
b 0
f 2
lcom 2
cbo 10
dl 0
loc 132
ccs 0
cts 59
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A registerAssets() 0 4 1
B run() 0 29 2
B processDelete() 0 29 5
A getViewPath() 0 6 2
1
<?php
2
/**
3
 * CommentListWidget.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.ru
6
 */
7
8
namespace rmrevin\yii\module\Comments\widgets;
9
10
use rmrevin\yii\module\Comments;
11
use yii\helpers\Html;
12
13
/**
14
 * Class CommentListWidget
15
 * @package rmrevin\yii\module\Comments\widgets
16
 */
17
class CommentListWidget extends \yii\base\Widget
18
{
19
20
    /** @var string|null */
21
    public $theme;
22
23
    /** @var string */
24
    public $viewFile = 'comment-list';
25
26
    /** @var array */
27
    public $viewParams = [];
28
29
    /** @var array */
30
    public $options = ['class' => 'comments-widget'];
31
32
    /** @var string */
33
    public $entity;
34
35
    /** @var string */
36
    public $anchorAfterUpdate = '#comment-%d';
37
38
    /** @var array */
39
    public $pagination = [
40
        'pageParam' => 'page',
41
        'pageSizeParam' => 'per-page',
42
        'pageSize' => 20,
43
        'pageSizeLimit' => [1, 50],
44
    ];
45
46
    /** @var array */
47
    public $sort = [
48
        'defaultOrder' => [
49
            'id' => SORT_ASC,
50
        ],
51
    ];
52
53
    /** @var bool */
54
    public $showDeleted = true;
55
56
    /** @var bool */
57
    public $showCreateForm = true;
58
59
    public function init()
60
    {
61
        parent::init();
62
63
        if (!isset($this->options['id'])) {
64
            $this->options['id'] = $this->getId();
65
        }
66
    }
67
68
    /**
69
     * Register asset bundle
70
     */
71
    protected function registerAssets()
72
    {
73
        CommentListAsset::register($this->getView());
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function run()
80
    {
81
        $this->registerAssets();
82
83
        $this->processDelete();
84
85
        /** @var Comments\models\Comment $CommentModel */
86
        $CommentModel = \Yii::createObject(Comments\Module::instance()->model('comment'));
87
        $CommentsQuery = $CommentModel::find()
88
            ->byEntity($this->entity);
89
90
        if (false === $this->showDeleted) {
91
            $CommentsQuery->withoutDeleted();
92
        }
93
94
        $CommentsDataProvider = \Yii::createObject([
95
            'class' => \yii\data\ActiveDataProvider::className(),
96
            'query' => $CommentsQuery->with(['author', 'lastUpdateAuthor']),
97
            'pagination' => $this->pagination,
98
            'sort' => $this->sort,
99
        ]);
100
101
        $params = $this->viewParams;
102
        $params['CommentsDataProvider'] = $CommentsDataProvider;
103
104
        $content = $this->render($this->viewFile, $params);
105
106
        return Html::tag('div', $content, $this->options);
107
    }
108
109
    private function processDelete()
110
    {
111
        $delete = (int)\Yii::$app->getRequest()->get('delete-comment');
112
        if ($delete > 0) {
113
114
            /** @var Comments\models\Comment $CommentModel */
115
            $CommentModel = \Yii::createObject(Comments\Module::instance()->model('comment'));
116
117
            /** @var Comments\models\Comment $Comment */
118
            $Comment = $CommentModel::find()
119
                ->byId($delete)
120
                ->one();
121
122
            if ($Comment->isDeleted()) {
123
                return;
124
            }
125
126
            if (!($Comment instanceof Comments\models\Comment)) {
127
                throw new \yii\web\NotFoundHttpException(\Yii::t('app', 'Comment not found.'));
128
            }
129
130
            if (!$Comment->canDelete()) {
131
                throw new \yii\web\ForbiddenHttpException(\Yii::t('app', 'Access Denied.'));
132
            }
133
134
            $Comment->deleted = $CommentModel::DELETED;
135
            $Comment->update();
136
        }
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    public function getViewPath()
143
    {
144
        return empty($this->theme)
145
            ? parent::getViewPath()
146
            : (\Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . $this->theme);
147
    }
148
}
149