Passed
Push — master ( e178e9...0a6cdc )
by Rutger
13:08
created

Oauth2ActiveRecordTrait::persist()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
ccs 3
cts 6
cp 0.5
rs 10
cc 3
nc 2
nop 2
crap 4.125
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
                'class' => static::class,
23
            ]));
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