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

Created   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setCreatedBehavior() 0 4 1
A initializeCreated() 0 13 1
A getCreatedBehavior() 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\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 Created
23
{
24
    use AbstractBehavior;
25
    use Options;
26
    use Identity;
27
    use Snapshots;
28
    use SoftDelete;
29
    
30
    public Transformable $createdBehavior;
31
    
32
    /**
33
     * Initializing Created
34
     */
35
    public function initializeCreated(?array $options = null): void
36
    {
37
        $options ??= $this->getOptionsManager()->get('created') ?? [];
38
        
39
        $fieldBy = $options['fieldBy'] ?? 'createdBy';
40
        $fieldAs = $options['fieldAs'] ?? 'createdAs';
41
        $fieldAt = $options['fieldAt'] ?? 'createdAt';
42
        
43
        $this->setCreatedBehavior(new Transformable([
44
            'beforeValidationOnCreate' => [
45
                $fieldBy => $this->getCurrentUserIdCallback(),
46
                $fieldAs => $this->getCurrentUserIdCallback(true),
47
                $fieldAt => date(Model::DATETIME_FORMAT),
48
            ],
49
        ]));
50
    }
51
    
52
    /**
53
     * Set Created Behavior
54
     */
55
    public function setCreatedBehavior(Transformable $createdBehavior): void
56
    {
57
        $this->createdBehavior = $createdBehavior;
58
        $this->addBehavior($this->createdBehavior);
59
    }
60
    
61
    /**
62
     * Get Created Behavior
63
     */
64
    public function getCreatedBehavior(): Transformable
65
    {
66
        return $this->createdBehavior;
67
    }
68
}
69