EDeleteAction::getModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Yarmaliuk Mikhail
5
 * Date: 20.12.2017
6
 * Time: 23:50
7
 */
8
9
namespace MP\ExtendedApi;
10
11
use Yii;
12
use yii\db\ActiveRecord;
13
use yii\rest\DeleteAction;
14
use yii\web\NotFoundHttpException;
15
use yii\web\ServerErrorHttpException;
16
17
/**
18
 * Class    EDeleteAction
19
 * @package MP\ExtendedApi
20
 * @author  Yarmaliuk Mikhail
21
 * @version 1.0
22
 */
23
class EDeleteAction extends DeleteAction
24
{
25
    /**
26
     * @var ActiveRecord|null
27
     */
28
    private ?ActiveRecord $_model;
29
30
    /**
31
     * Get deleted model
32
     *
33
     * @return ActiveRecord|null
34
     */
35
    public function getModel(): ?ActiveRecord
36
    {
37
        return $this->_model;
38
    }
39
40
    /**
41
     * Deletes a model.
42
     * @param mixed $id id of the model to be deleted.
43
     *
44
     * @throws NotFoundHttpException on failure.
45
     * @throws ServerErrorHttpException
46
     */
47
    public function run($id)
48
    {
49
        $model = $this->findModel($id);
50
51
        if ($this->checkAccess) {
52
            call_user_func($this->checkAccess, $this->id, $model);
53
        }
54
55
        $this->_model = $model;
56
57
        if ($model->delete() === false) {
58
            throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
59
        }
60
61
        Yii::$app->getResponse()->setStatusCode(204);
62
    }
63
}
64