SoftDeleteQueryBehavior   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 53.85%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 3
c 4
b 0
f 0
lcom 1
cbo 3
dl 0
loc 34
ccs 7
cts 13
cp 0.5385
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A deleted() 0 4 1
A notDeleted() 0 4 1
A tableName() 0 6 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 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