Passed
Push — master ( e9591a...c0a685 )
by Rutger
13:20
created

Oauth2ActiveRecordTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 31
ccs 10
cts 13
cp 0.7692
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A persist() 0 10 3
A findOrCreate() 0 11 2
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\models\traits;
4
5
use rhertogh\Yii2Oauth2Server\interfaces\models\base\Oauth2ActiveRecordInterface;
6
use Yii;
7
use yii\db\ActiveRecord;
8
use yii\db\Exception as DbException;
9
use yii\helpers\ArrayHelper;
10
11
trait Oauth2ActiveRecordTrait
12
{
13
    /**
14
     * @inheritDoc
15
     */
16 1
    public static function findOrCreate($condition)
17
    {
18 1
        $activeRecord = static::findOne($condition);
19
20 1
        if (empty($activeRecord)) {
21 1
            $activeRecord = Yii::createObject(ArrayHelper::merge($condition, [
22 1
                'class' => static::class,
23 1
            ]));
24
        }
25
26 1
        return $activeRecord;
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 1
    public function persist($runValidation = true, $attributeNames = null)
33
    {
34
        /** @var ActiveRecord|Oauth2ActiveRecordInterface $this */
35 1
        if (!$this->save($runValidation, $attributeNames)) {
36
            throw new DbException('Could not save ' . static::class .
37
                (YII_DEBUG ? PHP_EOL . print_r($this->attributes, true) : '') .
0 ignored issues
show
Bug introduced by
Are you sure print_r($this->attributes, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
                (YII_DEBUG ? PHP_EOL . /** @scrutinizer ignore-type */ print_r($this->attributes, true) : '') .
Loading history...
38
                ' Errors: ' . PHP_EOL . implode(', ', $this->getErrorSummary(true)));
39
        }
40
41 1
        return $this;
42
    }
43
}
44