FindRecordTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A findModel() 0 7 2
1
<?php
2
3
namespace yiicod\base\traits;
4
5
use yii\db\ActiveRecordInterface;
6
use yii\web\HttpException;
7
8
trait FindRecordTrait
9
{
10
    /**
11
     * Returns the data model based on the primary key given in the param.
12
     * If the data model is not found, an HTTP exception will be raised.
13
     *
14
     * @param int $id the ID of the model to be loaded
15
     * @param string $class
16
     *
17
     * @return ActiveRecordInterface the loaded model
18
     *
19
     * @throws HttpException
20
     */
21
    public function findModel($id, $class)
22
    {
23
        if ($model = $class::findOne($id)) {
24
            return $model;
25
        }
26
        throw new HttpException(404, 'The requested page does not exist.');
27
    }
28
}
29