SoftDeleteQueryBehavior::deleted()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 2
cts 4
cp 0.5
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1.125
1
<?php
2
3
namespace cornernote\softdelete;
4
5
use yii\base\Behavior;
6
use yii\db\ActiveQuery;
7
use yii\db\ActiveRecord;
8
9
/**
10
 * SoftDeleteQueryBehavior
11
 *
12
 * @usage:
13
 * ```
14
 * public function behaviors() {
15
 *     return [
16
 *         [
17
 *             'class' => 'cornernote\behaviors\SoftDeleteQueryBehavior',
18
 *             'attribute' => 'delete_time',
19
 *         ],
20
 *     ];
21
 * }
22
 * ```
23
 *
24
 * @property ActiveQuery $owner
25
 *
26
 * @author cornernote <[email protected]>
27
 */
28
class SoftDeleteQueryBehavior extends Behavior
29
{
30
    /**
31
     * @var string SoftDelete attribute
32
     */
33
    public $attribute = 'deleted_at';
34
35
    /**
36
     * @return static
37
     */
38 1
    public function deleted()
39
    {
40 1
        return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NOT NULL');
41
    }
42
43
    /**
44
     * @return static
45
     */
46 2
    public function notDeleted()
47
    {
48 2
        return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NULL');
49
    }
50
51
    /**
52
     * @return string
53
     */
54 3
    protected function tableName()
55
    {
56
        /** @var ActiveRecord $modelClass */
57 3
        $modelClass = $this->owner->modelClass;
58 3
        return $modelClass::tableName();
59
    }
60
61
}
62