Completed
Push — master ( e62bff...535998 )
by vistart
08:41
created

UserTrait::create()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0729

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 16
ccs 12
cts 14
cp 0.8571
rs 8.8571
cc 5
eloc 11
nc 8
nop 4
crap 5.0729
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 20
    public function create($className, $config = [], $loadDefault = true, $skipIfSet = true)
40
    {
41 20
        if (!isset($config['userClass'])) {
42 20
            $config['userClass'] = static::className();
43 20
        }
44 20
        if (isset($config['class'])) {
45
            unset($config['class']);
46
        }
47 20
        $entity = new $className($config);
48 20
        $createdByAttribute = $entity->createdByAttribute;
49 20
        $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 20
        if ($loadDefault && method_exists($entity, 'loadDefaultValues')) {
51 20
            $entity->loadDefaultValues($skipIfSet);
52 20
        }
53 20
        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 2
    public function findOneOrCreate($className, $condition = [], $config = null)
64
    {
65 2
        $entity = new $className(['skipInit' => true]);
66 2
        if (!isset($condition[$entity->createdByAttribute])) {
67 2
            $condition[$entity->createdByAttribute] = $this->guid;
68 2
        }
69 2
        $model = $className::findOne($condition);
70 2
        if (!$model) {
71 2
            if ($config === null || !is_array($config)) {
72 2
                $config = $condition;
73 2
            }
74 2
            $model = $this->create($className, $config);
75 2
        }
76 2
        return $model;
77
    }
78
79
    /**
80
     * Get all rules with current user properties.
81
     * @return array all rules.
82
     */
83 34
    public function rules()
84
    {
85 34
        return array_merge(
86 34
            parent::rules(),
87 34
            $this->passwordHashRules,
88 34
            $this->passwordResetTokenRules,
89 34
            $this->sourceRules,
90 34
            $this->statusRules,
91 34
            $this->authKeyRules,
92 34
            $this->accessTokenRules
93 34
        );
94
    }
95
}
96