Completed
Pull Request — master (#178)
by Corey
03:14
created

CustomBehaviorController::actionCreate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 4
nc 3
nop 0
1
<?php
2
namespace site\controllers;
3
4
5
use Yii;
6
use common\components\Controller;
7
use common\models\CustomBehavior;
8
use yii\filters\VerbFilter;
9
use common\components\AccessControl;
10
use kartik\grid\GridView;
11
use kartik\grid\EditableColumnAction;
12
13
14
15
/**
16
 * Custom Behavior controller
17
 */
18
class CustomBehaviorController extends Controller {
19
20
  public function behaviors() {
21
    return [
22
      'access' => [
23
        'class' => AccessControl::class,
24
        'rules' => [
25
          [
26
            'actions' => ['create', 'update', 'delete'],
27
            'allow'   => true,
28
            'roles'   => ['@'],
29
          ],
30
        ],
31
      ],
32
      'verbs' => [
33
        'class' => VerbFilter::class,
34
        'actions' => [
35
          'create' => ['post'],
36
          'update' => ['post'],
37
          'delete' => ['post'],
38
        ],
39
      ],
40
    ];
41
  }
42
43
  public function actions() {
44
    return array_replace_recursive(parent::actions(), [
45
      'update' => [  // identifier for your editable column action
46
        'class'           => EditableColumnAction::className(), // action class name
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

46
        'class'           => /** @scrutinizer ignore-deprecated */ EditableColumnAction::className(), // action class name

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
47
        'modelClass'      => CustoMBehavior::className(), // the model for the record being edited
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
        'modelClass'      => /** @scrutinizer ignore-deprecated */ CustoMBehavior::className(), // the model for the record being edited

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
48
        'scenario'        => CustomBehavior::SCENARIO_DEFAULT, // model scenario assigned before validation & update
49
        'showModelErrors' => true, // show model validation errors after save
50
        'errorOptions'    => ['header' => ''], // error summary HTML options
51
        'postOnly'        => true,
52
        'ajaxOnly'        => true,
53
        'findModel'       => function($id, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
        'findModel'       => function($id, /** @scrutinizer ignore-unused */ $action) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
          $model = $this->findModel($id);
55
          if($model !== null) {
56
            return $model;
57
          }
58
          throw new NotFoundHttpException('The specified behavior does not exist.');
0 ignored issues
show
Bug introduced by
The type site\controllers\NotFoundHttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
59
        },
60
      ]
61
    ]);
62
  }
63
64
  public function actionCreate() {
65
    $form = Yii::$container->get(\site\models\CustomBehaviorForm::class, [Yii::$app->user->identity]);
66
67
    if($form->load(Yii::$app->request->post()) && $form->validate()) {
68
      $form->save();
69
      return $this->redirect(['profile/index']);
70
    }
71
72
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
73
    if($errors = $form->getErrorSummary(true)) {
74
      return $errors;
75
    } else {
76
      return ["success" => false, "message" => "Unknown Error"];
77
    }
78
  }
79
80
  public function actionDelete(int $id) {
81
    $model = $this->findModel($id);
82
    $model->delete();
83
    return $this->renderContent($model->getGridView());
84
  }
85
86
  protected function findModel($id) {
87
    $model = CustomBehavior::findOne(['id' => $id, 'user_id' => Yii::$app->user->id]);
88
    if($model !== null) {
89
      return $model;
90
    }
91
92
    throw new NotFoundHttpException('The requested page does not exist.');
93
  }
94
}
95