Completed
Push — master ( d53907...a42add )
by vistart
05:33
created

BaseEntityModel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 77.27%

Importance

Changes 16
Bugs 1 Features 0
Metric Value
wmc 11
c 16
b 1
f 0
lcom 2
cbo 4
dl 0
loc 54
ccs 17
cts 22
cp 0.7727
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
B checkAttributes() 0 11 7
A find() 0 9 2
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\models;
14
15
use yii\db\ActiveRecord;
16
use vistart\Models\queries\BaseEntityQuery;
17
use vistart\Models\traits\EntityTrait;
18
19
/**
20
 * The abstract BaseEntityModel is used for entity model class which associates
21
 * with relational database table.
22
 * Note: the $idAttribute and $guidAttribute are not be assigned to false
23
 * simultaneously, and you should set at least one of them as primary key.
24
 * @version 2.0
25
 * @author vistart <[email protected]>
26
 */
27
abstract class BaseEntityModel extends ActiveRecord
28
{
29
    use EntityTrait;
30
31
    /**
32
     * Initialize new entity.
33
     */
34 36
    public function init()
35
    {
36 36
        if ($this->skipInit) {
37
            return;
38
        }
39 36
        $this->initEntityEvents();
40 36
        $this->checkAttributes();
41 36
        parent::init();
42 36
    }
43
44
    /**
45
     * Check whether all properties meet the standards. If you want to disable
46
     * checking, please override this method and return true directly. This
47
     * method runs when environment is not production or disable debug mode.
48
     * @return boolean true if all checks pass.
49
     * @throws \yii\base\NotSupportedException
50
     */
51 36
    public function checkAttributes()
52
    {
53 36
        if (YII_ENV !== YII_ENV_PROD || YII_DEBUG) {
54 36
            if (!is_string($this->idAttribute) && empty($this->idAttribute) &&
55 36
                !is_string($this->guidAttribute) && empty($this->guidAttribute)) {
56
                $errorInfo = 'ID and GUID attributes are not be disabled simultaneously in relational database.';
57
                throw new \yii\base\NotSupportedException($errorInfo);
58
            }
59 36
        }
60 36
        return true;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     * ------------
66
     * This static method will take $queryClass property to query class. If it is
67
     * not a string, The [[BaseEntityQuery]] will be taken.
68
     * @return \vistart\Models\models\BaseEntityQuery the newly created [[BaseEntityQuery]]
69
     * or its extended class instance.
70
     */
71 36
    public static function find()
72
    {
73 36
        $self = static::buildNoInitModel();
74 36
        if (!is_string($self->queryClass)) {
75
            $self->queryClass = BaseEntityQuery::className();
76
        }
77 36
        $queryClass = $self->queryClass;
78 36
        return new $queryClass(get_called_class(), ['noInitModel' => $self]);
79
    }
80
}
81