Test Failed
Push — master ( b1cf76...d8fe36 )
by Julien
08:28
created

Deleted::getDeletedBehavior()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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\Traits\Blameable;
13
14
use Zemit\Db\Column;
15
use Zemit\Mvc\Model\Behavior\Transformable;
16
use Zemit\Mvc\Model\Traits\Abstracts\AbstractBehavior;
17
use Zemit\Mvc\Model\Traits\Abstracts\AbstractBlameable;
18
use Zemit\Mvc\Model\Traits\Identity;
19
use Zemit\Mvc\Model\Traits\Options;
20
use Zemit\Mvc\Model\Traits\Snapshot;
21
use Zemit\Mvc\Model\Traits\SoftDelete;
22
23
trait Deleted
24
{
25
    use AbstractBehavior;
26
    use AbstractBlameable;
27
    use Options;
28
    use Identity;
29
    use Snapshot;
30
    use SoftDelete;
31
    use BlameAt;
32
    
33
    /**
34
     * Initializing Deleted
35
     */
36 56
    public function initializeDeleted(?array $options = null): void
37
    {
38 56
        $options ??= $this->getOptionsManager()->get('deleted') ?? [];
39
        
40 56
        $deletedField = $options['field'] ?? 'deleted';
41 56
        $deletedValue = $options['value'] ?? 1;
42
        
43 56
        $fieldBy = $options['fieldBy'] ?? 'deletedBy';
44 56
        $fieldAs = $options['fieldAs'] ?? 'deletedAs';
45 56
        $fieldAt = $options['fieldAt'] ?? 'deletedAt';
46
        
47 56
        $this->addUserRelationship($fieldBy, 'DeletedBy');
48 56
        $this->addUserRelationship($fieldAs, 'DeletedAs');
49
        
50 56
        $this->setDeletedBehavior(new Transformable([
51 56
            'beforeDelete' => [
52 56
                $fieldBy => $this->getCurrentUserIdCallback(),
53 56
                $fieldAs => $this->getCurrentUserIdCallback(true),
54 56
                $fieldAt => $this->getDateCallback(Column::DATETIME_FORMAT),
55 56
            ],
56 56
            'beforeValidationOnUpdate' => [
57 56
                $fieldBy => $this->hasChangedCallback(function ($model, $field) use ($deletedField, $deletedValue) {
58
                    return $model->isDeleted($deletedField, $deletedValue)
59
                        ? $this->getCurrentUserIdCallback()()
60
                        : $model->readAttribute($field);
61 56
                }),
62 56
                $fieldAs => $this->hasChangedCallback(function ($model, $field) use ($deletedField, $deletedValue) {
63
                    return $model->isDeleted($deletedField, $deletedValue)
64
                        ? $this->getCurrentUserIdCallback(true)()
65
                        : $model->readAttribute($field);
66 56
                }),
67 56
                $fieldAt => $this->hasChangedCallback(function ($model, $field) use ($deletedField, $deletedValue) {
68
                    return $model->isDeleted($deletedField, $deletedValue)
69
                        ? $this->getDateCallback(Column::DATETIME_FORMAT)
70
                        : $model->readAttribute($field);
71 56
                }),
72 56
            ],
73 56
        ]));
74
    }
75
    
76
    /**
77
     * Set Deleted Behavior
78
     */
79 56
    public function setDeletedBehavior(Transformable $deletedBehavior): void
80
    {
81 56
        $this->setBehavior('deleted', $deletedBehavior);
82
    }
83
    
84
    /**
85
     * Get Deleted Behavior
86
     */
87
    public function getDeletedBehavior(): Transformable
88
    {
89
        $behavior = $this->getBehavior('deleted');
90
        assert($behavior instanceof Transformable);
91
        return $behavior;
92
    }
93
}
94