CommentFormWidget   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 7
c 7
b 0
f 2
lcom 2
cbo 4
dl 0
loc 70
ccs 0
cts 34
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAssets() 0 4 1
B run() 0 30 4
A getViewPath() 0 6 2
1
<?php
2
/**
3
 * CommentFormWidget.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
12
/**
13
 * Class CommentFormWidget
14
 * @package rmrevin\yii\module\Comments\widgets
15
 */
16
class CommentFormWidget extends \yii\base\Widget
17
{
18
19
    /** @var string|null */
20
    public $theme;
21
22
    /** @var string */
23
    public $entity;
24
25
    /** @var Comments\models\Comment */
26
    public $Comment;
27
28
    /** @var string */
29
    public $anchor = '#comment-%d';
30
31
    /** @var string */
32
    public $viewFile = 'comment-form';
33
34
    /**
35
     * Register asset bundle
36
     */
37
    protected function registerAssets()
38
    {
39
        CommentFormAsset::register($this->getView());
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function run()
46
    {
47
        $this->registerAssets();
48
49
        /** @var Comments\forms\CommentCreateForm $CommentCreateForm */
50
        $CommentCreateFormClassData = Comments\Module::instance()->model(
51
            'commentCreateForm', [
52
                'Comment' => $this->Comment,
53
                'entity' => $this->entity,
54
            ]
55
        );
56
57
        $CommentCreateForm = \Yii::createObject($CommentCreateFormClassData);
58
59
        if ($CommentCreateForm->load(\Yii::$app->getRequest()->post())) {
60
            if ($CommentCreateForm->validate()) {
61
                if ($CommentCreateForm->save()) {
62
                    \Yii::$app->getResponse()
63
                        ->refresh(sprintf($this->anchor, $CommentCreateForm->Comment->id))
64
                        ->send();
65
66
                    exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
67
                }
68
            }
69
        }
70
71
        return $this->render($this->viewFile, [
72
            'CommentCreateForm' => $CommentCreateForm,
73
        ]);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function getViewPath()
80
    {
81
        return empty($this->theme)
82
            ? parent::getViewPath()
83
            : (\Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . $this->theme);
84
    }
85
}
86