Test Failed
Push — master ( 513669...a14d6b )
by Julien
09:38 queued 04:28
created

SoftDelete::setSoftDeleteBehavior()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior;
15
use Zemit\Mvc\Model\AbstractTrait\AbstractEntity;
16
use Zemit\Mvc\Model\AbstractTrait\AbstractEventsManager;
17
use Zemit\Mvc\Model\AbstractTrait\AbstractModelsManager;
18
19
trait SoftDelete
20
{
21
    use AbstractModelsManager;
22
    use AbstractBehavior;
23
    use AbstractEventsManager;
24
    use AbstractEntity;
25
    use Options;
26
    use Behavior;
27
    
28
    protected $skipped = false;
29
    
30
    /**
31
     * Initializing SoftDelete
32
     */
33 2
    public function initializeSoftDelete(?array $options = null): void
34
    {
35 2
        $options ??= $this->getOptionsManager()->get('softDelete') ?? [];
36
        
37 2
        $options['field'] ??= 'deleted';
38 2
        $options['value'] ??= 1;
39
        
40 2
        $this->setSoftDeleteBehavior(new Behavior\SoftDelete($options));
41
    }
42
    
43
    /**
44
     * Set the SoftDeleteBehavior variable
45
     * Attach the SoftDelete behavior class
46
     */
47 2
    public function setSoftDeleteBehavior(Behavior\SoftDelete $softDeleteBehavior): void
48
    {
49 2
        $this->setBehavior('softDelete', $softDeleteBehavior);
50
    }
51
    
52
    /**
53
     * Return the soft delete behavior instance
54
     */
55
    public function getSoftDeleteBehavior(): Behavior\SoftDelete
56
    {
57
        $behavior = $this->getBehavior('softDelete');
58
        assert($behavior instanceof Behavior\SoftDelete);
59
        return $behavior;
60
    }
61
    
62
    /**
63
     * Disable the soft delete for the current instance
64
     * Note: Zemit SoftDelete behavior must be attached
65
     */
66
    public function disableSoftDelete(): void
67
    {
68
        $this->getSoftDeleteBehavior()->disable();
69
    }
70
    
71
    /**
72
     * Enable the soft delete for the current instance
73
     * Note: Zemit SoftDelete behavior must be attached
74
     */
75
    public function enableSoftDelete(): void
76
    {
77
        $this->getSoftDeleteBehavior()->enable();
78
    }
79
    
80
    /**
81
     * Helper method to check if the row is soft deleted
82
     */
83 1
    public function isDeleted(?string $field = null, ?int $deletedValue = null): bool
84
    {
85 1
        $field ??= $this->getSoftDeleteBehavior()->getField() ?? 'deleted';
86 1
        $deletedValue ??= $this->getSoftDeleteBehavior()->getValue() ?? 1;
87 1
        return $this->readAttribute($field) === $deletedValue;
88
    }
89
    
90
    /**
91
     * Restore a previously Soft-deleted entry and fire events
92
     * Events:
93
     * - beforeRestore
94
     * - notRestored
95
     * - afterRestore
96
     *
97
     * @todo add a check from orm.events setup state
98
     */
99
    public function restore(?string $field = null, ?int $notDeletedValue = null): bool
100
    {
101
        $ormEvents = (bool)ini_get('phalcon.orm.events');
102
        
103
        if ($ormEvents) {
104
            $this->skipped = false;
105
            
106
            // fire event, allowing to stop options or skip the current operation
107
            if ($this->fireEventCancel('beforeRestore') === false) {
108
                return false;
109
            }
110
            
111
            if ($this->skipped) {
112
                return true;
113
            }
114
        }
115
        
116
        $field ??= $this->getSoftDeleteBehavior()->getField();
117
        $notDeletedValue ??= 0;
118
        
119
        // restore (unset soft delete value and save)
120
        $this->writeAttribute($field, $notDeletedValue);
121
        $save = $this->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? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        /** @scrutinizer ignore-call */ 
122
        $save = $this->save();
Loading history...
122
        
123
        // check if the entity is restored
124
        $value = $this->readAttribute($field);
125
        $restored = $save && $value === $notDeletedValue;
126
        
127
        // fire events
128
        if ($ormEvents) {
129
            if (!$restored) {
130
                $this->fireEvent('notRestored');
131
            }
132
            else {
133
                $this->fireEvent('afterRestore');
134
            }
135
        }
136
        
137
        return $restored;
138
    }
139
}
140