Passed
Push — master ( d63ab9...9b5ad1 )
by Anton
28s
created

DeleteAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 15 3
1
<?php
2
/**
3
 * @author Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\jsonapi\actions;
7
8
use Yii;
9
use yii\db\BaseActiveRecord;
10
use yii\web\NotFoundHttpException;
11
use yii\web\ServerErrorHttpException;
12
13
/**
14
 * Implements the API endpoint for deleting resources.
15
 * @link http://jsonapi.org/format/#crud-deleting
16
 */
17
class DeleteAction extends Action
18
{
19
    /**
20
     * Deletes a resource.
21
     * @param mixed $id id of the resource to be deleted.
22
     * @throws NotFoundHttpException
23
     * @throws ServerErrorHttpException on failure.
24
     */
25
    public function run($id)
26
    {
27
        /** @var BaseActiveRecord $model */
28
        $model = $this->findModel($id);
29
30
        if ($this->checkAccess) {
31
            call_user_func($this->checkAccess, $this->id, $model);
32
        }
33
34
        if ($model->delete() === false) {
35
            throw new ServerErrorHttpException('Failed to delete the resource for unknown reason.');
36
        }
37
38
        Yii::$app->getResponse()->setStatusCode(204);
39
    }
40
}