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

Updated::initializeUpdated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 18
rs 9.8666
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 Updated
23
{
24
    use AbstractBehavior;
25
    use Options;
26
    use Identity;
27
    use Snapshots;
28
    use SoftDelete;
29
    
30
    public Transformable $updatedBehavior;
31
    
32
    /**
33
     * Initializing Updated
34
     */
35
    public function initializeUpdated(?array $options = null): void
36
    {
37
        $options ??= $this->getOptionsManager()->get('updated') ?? [];
38
        
39
        $fieldBy = $options['fieldBy'] ?? 'updatedBy';
40
        $fieldAs = $options['fieldAs'] ?? 'updatedAs';
41
        $fieldAt = $options['fieldAt'] ?? 'updatedAt';
42
        
43
        $this->setUpdatedBehavior(new Transformable([
44
            'beforeValidationOnUpdate' => [
45
                $fieldBy => $this->hasChangedCallback(function () {
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

45
                $fieldBy => $this->/** @scrutinizer ignore-call */ hasChangedCallback(function () {
Loading history...
46
                    return $this->getCurrentUserIdCallback(false)();
47
                }),
48
                $fieldAs => $this->hasChangedCallback(function () {
49
                    return $this->getCurrentUserIdCallback(true)();
50
                }),
51
                $fieldAt => $this->hasChangedCallback(function () {
52
                    return date(Model::DATETIME_FORMAT);
53
                }),
54
            ],
55
        ]));
56
    }
57
    
58
    /**
59
     * Set Updated Behavior
60
     */
61
    public function setUpdatedBehavior(Transformable $updatedBehavior): void
62
    {
63
        $this->updatedBehavior = $updatedBehavior;
64
        $this->addBehavior($this->updatedBehavior);
65
    }
66
    
67
    /**
68
     * Get Updated Behavior
69
     */
70
    public function getUpdatedBehavior(): Transformable
71
    {
72
        return $this->updatedBehavior;
73
    }
74
}
75