GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MassPublishAction   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 19.23 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 10
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 10 10 3
B run() 0 28 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * this Action provides group publish and unpublish functionality.
4
 * Usage example: in backend controller
5
 * public function actions()
6
 * {
7
 *  return [
8
 *  ...
9
 *      'publish-switch' => [
10
 *          'class' => app\backend\actions\MassPublishAction::className(),
11
 *          'modelName' => Page::className(),
12
 *          'attribute' => 'published',
13
 *      ]
14
 * ];
15
 * @property string $modelName must be name of existing Model class. This property is required
16
 * @property string $attribute name of model attribute that provides public item visibility
17
 */
18
19
namespace app\backend\actions;
20
21
use Yii;
22
use yii\base\Action;
23
use yii\web\Response;
24
use yii\web\NotFoundHttpException;
25
use app\backend\widgets\PublishSwitchButtons;
26
use yii\web\ServerErrorHttpException;
27
28
class MassPublishAction extends Action
29
{
30
    public $modelName = '';
31
    public $attribute = '';
32
33
    /**
34
     * @throws ServerErrorHttpException
35
     */
36 View Code Duplication
    public function init()
37
    {
38
        parent::init();
39
        if (false === is_subclass_of($this->modelName, '\yii\db\ActiveRecord')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
40
            throw new ServerErrorHttpException('Model class does not exists');
41
        }
42
        if (false === in_array($this->attribute, (new $this->modelName)->attributes())) {
43
            throw new ServerErrorHttpException("Model '{$this->modelName}' has no '{$this->attribute}' field'" );
44
        }
45
    }
46
47
    /**
48
     * @return int
49
     * @throws NotFoundHttpException
50
     */
51
    public function run()
52
    {
53
        if (false === Yii::$app->request->isAjax) {
54
            throw new NotFoundHttpException();
55
        }
56
        Yii::$app->response->format = Response::FORMAT_JSON;
57
        $items = is_array(Yii::$app->request->post('ps-items')) ? Yii::$app->request->post('ps-items') : [];
58
        $mode = Yii::$app->request->post('ps-action');
59
        $active = null;
60
        $message = '';
61
        switch ($mode) {
62
            case PublishSwitchButtons::MASS_PUBLISH :
63
                $message = 'Items published: {n}';
64
                $active = 1;
65
                break;
66
            case PublishSwitchButtons::MASS_UNPUBLISH :
67
                $message = 'Items unpublished: {n}';
68
                $active = 0;
69
                break;
70
        }
71
        if (false === empty($items) && null !== $active) {
72
            $class = $this->modelName;
73
            $num = $class::updateAll([$this->attribute => $active],['id' => $items]);
74
            Yii::$app->session->setFlash('info', Yii::t('app', $message, ['n' => $num]));
75
            return 1;
76
        }
77
        return 0;
78
    }
79
}