Completed
Push — master ( 567ea1...6214df )
by Igor
05:17
created

ModelTrait::findModel()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace app\traits;
4
5
use app\helpers\Http;
6
7
trait ModelTrait
8
{
9
    /**
10
     * Find the model based on its primary key value or WHERE condition.
11
     * If the model is not found or access denied, a 404 HTTP exception will be thrown.
12
     *
13
     * @param ActiveRecord $model
14
     * @param int|array $id primary key or WHERE condition
15
     * @param callable $checkAccess
16
     * @return ActiveRecord
17
     * @throws NotFoundHttpException
18
     */
19
    public function findModel($model, $id, $checkAccess = null)
20
    {
21
        $model = $model::findOne($id);
22
23
        if ($model === null || ($checkAccess !== null && !$model->$checkAccess())) {
24
            Http::exception(404);
25
        } // @codeCoverageIgnore
26
27
        return $model;
28
    }
29
}
30