Test Failed
Push — master ( 894c40...e5d2d2 )
by Julien
11:34
created

SoftDelete   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 113
ccs 0
cts 33
cp 0
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setSoftDeleteBehavior() 0 4 1
A initializeSoftDelete() 0 4 1
A isDeleted() 0 5 1
A disableSoftDelete() 0 3 1
A enableSoftDelete() 0 3 1
B restore() 0 40 7
A getSoftDeleteBehavior() 0 3 1
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\AbstractEventsManager;
16
use Zemit\Mvc\Model\Behavior;
17
18
trait SoftDelete
19
{
20
    use AbstractBehavior;
21
    use AbstractEventsManager;
22
    use Attribute;
23
    
24
    public Behavior\SoftDelete $softDeleteBehavior;
25
    
26
    /**
27
     * Initializing SoftDelete
28
     */
29
    public function initializeSoftDelete(array $options = []): void
30
    {
31
        $options ??= $this->getOptionsManager()->get('softDelete');
0 ignored issues
show
Bug introduced by
It seems like getOptionsManager() 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

31
        $options ??= $this->/** @scrutinizer ignore-call */ getOptionsManager()->get('softDelete');
Loading history...
32
        $this->setSoftDeleteBehavior(new Behavior\SoftDelete($options));
33
    }
34
    
35
    /**
36
     * Return the soft delete behavior instance
37
     */
38
    public function getSoftDeleteBehavior(): ?Behavior\SoftDelete
39
    {
40
        return $this->softDeleteBehavior;
41
    }
42
    
43
    /**
44
     * Set the SoftDeleteBehavior variable
45
     * Attach the SoftDelete behavior class
46
     */
47
    public function setSoftDeleteBehavior(Behavior\SoftDelete $softDeleteBehavior): void
48
    {
49
        $this->softDeleteBehavior = $softDeleteBehavior;
50
        $this->addBehavior($softDeleteBehavior);
51
    }
52
    
53
    /**
54
     * Disable the soft delete for the current instance
55
     * Note: Zemit SoftDelete behavior must be attached
56
     */
57
    public function disableSoftDelete(): void
58
    {
59
        $this->getSoftDeleteBehavior()->disable();
60
    }
61
    
62
    /**
63
     * Enable the soft delete for the current instance
64
     * Note: Zemit SoftDelete behavior must be attached
65
     */
66
    public function enableSoftDelete(): void
67
    {
68
        $this->getSoftDeleteBehavior()->enable();
69
    }
70
    
71
    /**
72
     * Helper method to check if the row is soft deleted
73
     */
74
    public function isDeleted(?string $field = null, ?int $deletedValue = null): bool
75
    {
76
        $field ??= self::DELETED_FIELD;
0 ignored issues
show
Bug introduced by
The constant Zemit\Mvc\Model\SoftDelete::DELETED_FIELD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
77
        $deletedValue ??= self::YES;
0 ignored issues
show
Bug introduced by
The constant Zemit\Mvc\Model\SoftDelete::YES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
        return $this->getAttribute($field) === $deletedValue;
79
    }
80
    
81
    /**
82
     * Restore a previously Soft-deleted entry
83
     * Events:
84
     * - beforeRestore
85
     * - notRestored
86
     * - afterRestore
87
     *
88
     * @throws \Exception
89
     * @todo add a check from orm.events setup state
90
     */
91
    public function restore(?string $field = null, ?int $notDeletedValue = null): bool
92
    {
93
        $ormEvents = (bool)ini_get('orm.events');
94
        
95
        if ($ormEvents) {
96
            $this->skipped = false;
0 ignored issues
show
Bug Best Practice introduced by
The property skipped does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
97
            
98
            // fire event, allowing to stop options or skip the current operation
99
            if ($this->fireEventCancel('beforeRestore') === false) {
100
                return false;
101
            }
102
            
103
            if ($this->skipped) {
104
                return true;
105
            }
106
        }
107
        
108
        // get settings
109
        $field ??= self::DELETED_FIELD;
0 ignored issues
show
Bug introduced by
The constant Zemit\Mvc\Model\SoftDelete::DELETED_FIELD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
110
        $notDeletedValue ??= self::NO;
0 ignored issues
show
Bug introduced by
The constant Zemit\Mvc\Model\SoftDelete::NO was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
111
        
112
        // restore
113
        $this->assign([$field => $notDeletedValue], [$field]);
0 ignored issues
show
Bug introduced by
It seems like assign() 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

113
        $this->/** @scrutinizer ignore-call */ 
114
               assign([$field => $notDeletedValue], [$field]);
Loading history...
114
        $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

114
        /** @scrutinizer ignore-call */ 
115
        $save = $this->save();
Loading history...
115
        
116
        // check if the entity was really restored
117
        $value = $this->getAttribute($field);
118
        $restored = $save && $value === $notDeletedValue;
119
        
120
        // fire events
121
        if ($ormEvents) {
122
            if (!$restored) {
123
                $this->fireEvent('notRestored');
124
            }
125
            else {
126
                $this->fireEvent('afterRestore');
127
            }
128
        }
129
        
130
        return $restored;
131
    }
132
}
133