Test Failed
Push — master ( ce60e5...378563 )
by Julien
12:41 queued 07:49
created

Deleted::initializeDeleted()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4.432

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 35
ccs 21
cts 30
cp 0.7
rs 9.504
cc 4
nc 1
nop 1
crap 4.432
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\Db\Column;
15
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior;
16
use Zemit\Mvc\Model\AbstractTrait\AbstractBlameable;
17
use Zemit\Mvc\Model\Behavior\Transformable;
18
use Zemit\Mvc\Model\Identity;
19
use Zemit\Mvc\Model\Options;
20
use Zemit\Mvc\Model\Snapshot;
21
use Zemit\Mvc\Model\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 2
    public function initializeDeleted(?array $options = null): void
37
    {
38 2
        $options ??= $this->getOptionsManager()->get('deleted') ?? [];
39
        
40 2
        $deletedField = $options['field'] ?? 'deleted';
41 2
        $deletedValue = $options['value'] ?? 1;
42
        
43 2
        $fieldBy = $options['fieldBy'] ?? 'deletedBy';
44 2
        $fieldAs = $options['fieldAs'] ?? 'deletedAs';
45 2
        $fieldAt = $options['fieldAt'] ?? 'deletedAt';
46
        
47 2
        $this->addUserRelationship($fieldBy, 'DeletedBy');
48 2
        $this->addUserRelationship($fieldAs, 'DeletedAs');
49
        
50 2
        $this->setDeletedBehavior(new Transformable([
51 2
            'beforeDelete' => [
52 2
                $fieldBy => $this->getCurrentUserIdCallback(),
53 2
                $fieldAs => $this->getCurrentUserIdCallback(true),
54 2
                $fieldAt => $this->getDateCallback(Column::DATETIME_FORMAT),
55 2
            ],
56 2
            'beforeValidationOnUpdate' => [
57 2
                $fieldBy => $this->hasChangedCallback(function ($model, $field) use ($deletedField, $deletedValue) {
58
                    return $model->isDeleted($deletedField, $deletedValue)
59
                        ? $this->getCurrentUserIdCallback()()
60
                        : $model->readAttribute($field);
61 2
                }),
62 2
                $fieldAs => $this->hasChangedCallback(function ($model, $field) use ($deletedField, $deletedValue) {
63
                    return $model->isDeleted($deletedField, $deletedValue)
64
                        ? $this->getCurrentUserIdCallback(true)()
65
                        : $model->readAttribute($field);
66 2
                }),
67 2
                $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 2
                }),
72 2
            ],
73 2
        ]));
74
    }
75
    
76
    /**
77
     * Set Deleted Behavior
78
     */
79 2
    public function setDeletedBehavior(Transformable $deletedBehavior): void
80
    {
81 2
        $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