Completed
Push — master ( 221adf...efa5f6 )
by Brett
03:26
created

SoftDeleteQueryBehavior::tableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
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 3
    public function deleted()
39
    {
40 3
        return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NOT NULL');
41
    }
42
43
    /**
44
     * @return static
45
     */
46 6
    public function notDeleted()
47
    {
48 6
        return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NULL');
49
    }
50
51
    /**
52
     * @return string
53
     */
54 9
    protected function tableName()
55
    {
56
        /** @var ActiveRecord $modelClass */
57 9
        $modelClass = $this->owner->modelClass;
58 9
        return $modelClass::tableName();
59
    }
60
61
}
62