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::run()   B
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5386
c 0
b 0
f 0
cc 7
nc 13
nop 0
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
}