Test Failed
Push — master ( 72bdce...99bbeb )
by Julien
04:45
created

Blameable::initializeBlameable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 15
ccs 0
cts 7
cp 0
crap 2
rs 9.9666
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;
13
14
use Phalcon\Mvc\Model\Relation;
15
use Zemit\Config\ConfigInterface;
16
use Zemit\Models\Audit;
17
use Zemit\Models\AuditDetail;
18
use Zemit\Models\User;
19
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior;
20
use Zemit\Mvc\Model\AbstractTrait\AbstractIdentity;
0 ignored issues
show
Bug introduced by
The type Zemit\Mvc\Model\AbstractTrait\AbstractIdentity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Zemit\Mvc\Model\AbstractTrait\AbstractInjectable;
22
use Zemit\Mvc\Model\Behavior\Blameable as BlameableBehavior;
23
24
trait Blameable
25
{
26
    use AbstractBehavior;
27
    use AbstractIdentity;
28
    use AbstractInjectable;
29
    
30
    use Options;
31
    
32
    use Blameable\Created;
33
    use Blameable\Updated;
34
    use Blameable\Deleted;
35
    use Blameable\Restored;
36
    
37
    /**
38
     * Initializing Blameable
39
     */
40
    public function initializeBlameable(?array $options = null): void
41
    {
42
        $options ??= $this->getOptionsManager()->get('blameable') ?? [];
43
        
44
        $config = $this->getDI()->get('config');
45
        assert($config instanceof ConfigInterface);
46
        
47
        $options['auditClass'] ??= $config->getModelClass(Audit::class);
48
        $options['auditDetailClass'] ??= $config->getModelClass(AuditDetail::class);
49
        $options['userClass'] ??= $config->getModelClass(User::class);
50
        
51
        $this->setBlameableBehavior(new BlameableBehavior($options));
52
        
53
        $userField = $options['userField'] ?? 'userId';
54
        $this->addUserRelationship($userField, 'User');
55
    }
56
    
57
    /**
58
     * Set Blameable Behavior
59
     */
60
    public function setBlameableBehavior(BlameableBehavior $blameableBehavior): void
61
    {
62
        $this->setBehavior('blameable', $blameableBehavior);
63
    }
64
    
65
    /**
66
     * Get Blameable Behavior
67
     */
68
    public function getBlameableBehavior(): BlameableBehavior
69
    {
70
        $blameableBehavior = $this->getBehavior('blameable');
71
        assert($blameableBehavior instanceof BlameableBehavior);
72
        return $blameableBehavior;
73
    }
74
    
75
    /**
76
     * Add a user relationship
77
     */
78
    public function addUserRelationship(
79
        string $field = 'userId',
80
        string $alias = 'UserEntity',
81
        array $params = [],
82
        string $ref = 'id',
83
        string $type = 'belongsTo',
84
        ?string $class = null
85
    ): ?Relation {
86
        if (property_exists($this, $field)) {
87
            $class ??= $this->getIdentity()->getUserClass() ?: $this->getDI()->get('config')->getModelClass(\Zemit\Models\User::class);
88
            return $this->$type($field, $class, $ref, ['alias' => $alias, 'params ' => $params]);
89
        }
90
        
91
        return null;
92
    }
93
}
94