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

DeleteAction::run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 1
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
}