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

ModelTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A findModel() 0 10 4
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