Completed
Push — master ( 69007e...64430c )
by vistart
05:40
created

UserTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 94.59%

Importance

Changes 14
Bugs 0 Features 0
Metric Value
wmc 10
c 14
b 0
f 0
lcom 2
cbo 3
dl 0
loc 75
ccs 35
cts 37
cp 0.9459
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 4
B findOneOrCreate() 0 15 5
A rules() 0 12 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
/**
16
 * Assemble PasswordTrait, RegistrationTrait and IdentityTrait into UserTrait.
17
 *
18
 * @version 2.0
19
 * @author vistart <[email protected]>
20
 */
21
trait UserTrait
22
{
23
    use PasswordTrait,
24
        RegistrationTrait,
25
        IdentityTrait;
26
27
    /**
28
     * Create new entity model associated with current user.
29
     * if $config does not specify `userClass` property, self will be assigned to.
30
     * @param string $className Full qualified class name.
31
     * @param array $config name-value pairs that will be used to initialize
32
     * the object properties.
33
     * @param boolean $loadDefault Determines whether loading default values
34
     * after entity model created.
35
     * @param boolean $skipIfSet whether existing value should be preserved.
36
     * This will only set defaults for attributes that are `null`.
37
     * @return $className
0 ignored issues
show
Documentation introduced by
The doc-type $className could not be parsed: Unknown type name "$className" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
38
     */
39 19
    public function create($className, $config = [], $loadDefault = true, $skipIfSet = true)
40
    {
41 19
        if (!isset($config['userClass'])) {
42 19
            $config['userClass'] = static::className();
43 19
        }
44 19
        if (isset($config['class'])) {
45
            unset($config['class']);
46
        }
47 19
        $entity = new $className($config);
48 19
        $createdByAttribute = $entity->createdByAttribute;
49 19
        $entity->$createdByAttribute = $this->guid;
0 ignored issues
show
Bug introduced by
The property guid does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50 19
        if ($loadDefault) {
51 19
            $entity->loadDefaultValues($skipIfSet);
52 19
        }
53 19
        return $entity;
54
    }
55
56
    /**
57
     * Find existed or create new model.
58
     * @param string $className
59
     * @param array $condition
60
     * @param array $config
61
     * @return $className
0 ignored issues
show
Documentation introduced by
The doc-type $className could not be parsed: Unknown type name "$className" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
62
     */
63 1
    public function findOneOrCreate($className, $condition = [], $config = null)
64
    {
65 1
        $entity = new $className(['skipInit' => true]);
66 1
        if (!isset($condition[$entity->createdByAttribute])) {
67 1
            $condition[$entity->createdByAttribute] = $this->guid;
68 1
        }
69 1
        $model = $className::findOne($condition);
70 1
        if (!$model) {
71 1
            if ($config === null || !is_array($config)) {
72 1
                $config = $condition;
73 1
            }
74 1
            $model = $this->create($className, $config);
75 1
        }
76 1
        return $model;
77
    }
78
79
    /**
80
     * Get all rules with current user properties.
81
     * @return array all rules.
82
     */
83 33
    public function rules()
84
    {
85 33
        return array_merge(
86 33
            parent::rules(),
87 33
            $this->passwordHashRules,
88 33
            $this->passwordResetTokenRules,
89 33
            $this->sourceRules,
90 33
            $this->statusRules,
91 33
            $this->authKeyRules,
92 33
            $this->accessTokenRules
93 33
        );
94
    }
95
}
96