DeactivationTrait::markForActivation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Arrilot\BitrixModels\Models\Traits;
4
5
use Arrilot\BitrixModels\Models\D7Model;
6
use Bitrix\Main\Type\DateTime;
7
8
trait DeactivationTrait
9
{
10
    /**
11
     * Active element.
12
     */
13
    public function activate()
14
    {
15
        $this->markForActivation()->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
16
    }
17
18
    /**
19
     * Deactivate element.
20
     */
21
    public function deactivate()
22
    {
23
        $this->markForDeactivation()->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
24
    }
25
26
    /**
27
     * @param $query
28
     * @return mixed
29
     */
30
    public function scopeActive($query)
31
    {
32
        return $this instanceof D7Model
33
            ? $query->filter(['==UF_DEACTIVATED_AT' => null])
34
            : $query->whereNull('UF_DEACTIVATED_AT');
35
    }
36
    
37
    /**
38
     * @param $query
39
     * @return mixed
40
     */
41
    public function scopeDeactivated($query)
42
    {
43
        return $this instanceof D7Model
44
            ? $query->filter(['!==UF_DEACTIVATED_AT' => null])
45
            : $query->whereNotNull('UF_DEACTIVATED_AT');
46
    }
47
    
48
    /**
49
     * @return $this
50
     */
51
    public function markForActivation()
52
    {
53
        $this['UF_DEACTIVATED_AT'] = null;
54
55
        return $this;
56
    }
57
    
58
    /**
59
     * @return $this
60
     */
61
    public function markForDeactivation()
62
    {
63
        $this['UF_DEACTIVATED_AT'] = $this instanceof D7Model ? new DateTime() : date('Y-m-d H:i:s');
64
65
        return $this;
66
    }
67
}
68