HtmlWidget::run()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 6
eloc 10
nc 10
nop 0
1
<?php
2
/**
3
 * @link http://www.diemeisterei.de/
4
 *
5
 * @copyright Copyright (c) 2015 diemeisterei GmbH, Stuttgart
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace app\modules\prototype\widgets;
11
12
use yii\base\Widget;
13
use yii\helpers\Html;
14
15
class HtmlWidget extends Widget
16
{
17
    const SETTINGS_SECTION = 'app.html';
18
    const ACCESS_ROLE = 'Editor';
19
20
    public $key = null;
21
    public $enableFlash = false;
22
23
    public function run()
24
    {
25
        $html = \app\modules\prototype\models\Html::findOne(['key' => $this->generateKey()]);
26
27
        if (\Yii::$app->user->can(self::ACCESS_ROLE)) {
28
            $link = ($html) ? $this->generateEditLink($html->id) : $this->generateCreateLink();
29
            if ($this->enableFlash) {
30
                \Yii::$app->session->addFlash(
31
                    ($html) ? 'success' : 'info',
32
                    "Edit contents in {$link}, key: <code>{$this->generateKey()}</code>"
33
                );
34
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
35
36
            }
37
        }
38
39
        return $html ? $html->value : null;
40
    }
41
42
    private function generateKey()
43
    {
44
        if ($this->key) {
45
            return $this->key;
46
        } else {
47
            $key = \Yii::$app->request->getQueryParam('id');
48
        }
49
        return \Yii::$app->language.'/'.\Yii::$app->controller->route.($key ? '/'.$key : '');
50
    }
51
52
    private function generateCreateLink()
53
    {
54
        return Html::a('prototype module', ['/prototype/html/create', 'Html' => ['key' => $this->generateKey()]]);
55
    }
56
57
    private function generateEditLink($id)
58
    {
59
        return Html::a('prototype module', ['/prototype/html/update', 'id' => $id]);
60
    }
61
}
62