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

Deleted   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDeletedBehavior() 0 4 1
A getDeletedBehavior() 0 3 1
A initializeDeleted() 0 29 4
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\Blameable;
13
14
use Zemit\Mvc\Model;
15
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior;
16
use Zemit\Mvc\Model\Behavior\Transformable;
17
use Zemit\Mvc\Model\Identity;
18
use Zemit\Mvc\Model\Options;
19
use Zemit\Mvc\Model\Snapshots;
20
use Zemit\Mvc\Model\SoftDelete;
21
22
trait Deleted
23
{
24
    use AbstractBehavior;
25
    use Options;
26
    use Identity;
27
    use Snapshots;
28
    use SoftDelete;
29
    
30
    public Transformable $deletedBehavior;
31
    
32
    /**
33
     * Initializing Deleted
34
     */
35
    public function initializeDeleted(?array $options = null): void
36
    {
37
        $options ??= $this->getOptionsManager()->get('deleted') ?? [];
38
        
39
        $fieldBy = $options['fieldBy'] ?? 'deletedBy';
40
        $fieldAs = $options['fieldAs'] ?? 'deletedAs';
41
        $fieldAt = $options['fieldAt'] ?? 'deletedAt';
42
        
43
        $this->setDeletedBehavior(new Transformable([
44
            'beforeDelete' => [
45
                $fieldBy => $this->getCurrentUserIdCallback(false),
46
                $fieldAs => $this->getCurrentUserIdCallback(true),
47
                $fieldAt => date(Model::DATETIME_FORMAT),
48
            ],
49
            'beforeValidationOnUpdate' => [
50
                $fieldBy => $this->hasChangedCallback(function ($model, $field) {
0 ignored issues
show
Bug introduced by
It seems like hasChangedCallback() 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

50
                $fieldBy => $this->/** @scrutinizer ignore-call */ hasChangedCallback(function ($model, $field) {
Loading history...
51
                    return $model->isDeleted()
52
                        ? $this->getCurrentUserIdCallback()()
53
                        : $model->readAttribute($field);
54
                }),
55
                $fieldAs => $this->hasChangedCallback(function ($model, $field) {
56
                    return $model->isDeleted()
57
                        ? $this->getCurrentUserIdCallback(true)()
58
                        : $model->readAttribute($field);
59
                }),
60
                $fieldAt => $this->hasChangedCallback(function ($model, $field) {
61
                    return $model->isDeleted()
62
                        ? date(Model::DATETIME_FORMAT)
63
                        : $model->readAttribute($field);
64
                }),
65
            ],
66
        ]));
67
    }
68
    
69
    /**
70
     * Set Deleted Behavior
71
     */
72
    public function setDeletedBehavior(Transformable $deletedBehavior): void
73
    {
74
        $this->deletedBehavior = $deletedBehavior;
75
        $this->addBehavior($this->deletedBehavior);
76
    }
77
    
78
    /**
79
     * Get Deleted Behavior
80
     */
81
    public function getDeletedBehavior(): Transformable
82
    {
83
        return $this->deletedBehavior;
84
    }
85
}
86