Completed
Push — master ( f924c2...208563 )
by vistart
04:02
created

UserUsernameTrait::createUsername()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 9
cts 10
cp 0.9
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
crap 3.009
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\models;
14
15
use rhosocial\base\models\queries\BaseBlameableQuery;
16
17
/**
18
 * Trait UserUsernameTrait
19
 *
20
 * @property string|Username $username
21
 * @package rhosocial\user\models
22
 * @version 1.0
23
 * @author vistart <[email protected]>
24
 */
25
trait UserUsernameTrait
26
{
27
    public $usernameClass = false;
28
29
    /**
30
     * Check whether this user enables the username feature or not.
31
     * @return boolean
32
     */
33 4
    public function hasEnabledUsername()
34
    {
35 4
        if ($this->usernameClass === false || !is_string($this->usernameClass) || !class_exists($this->usernameClass)) {
36
            return false;
37
        }
38 4
        return true;
39
    }
40
41
    /**
42
     * Get username.
43
     * This method may return null, please consider processing the abnormal conditions.
44
     * @return BaseBlameableQuery
45
     */
46 4
    public function getUsername()
47
    {
48 4
        if (!$this->hasEnabledUsername()) {
49
            return null;
50
        }
51 4
        $usernameClass = $this->usernameClass;
52 4
        $noInit = $usernameClass::buildNoInitModel();
53
        /* @var $noInit Username */
54 4
        return $this->hasOne($usernameClass, [$noInit->createdByAttribute => $this->guidAttribute]);
0 ignored issues
show
Bug introduced by
The property guidAttribute 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...
Bug introduced by
It seems like hasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
    }
56
57
    /**
58
     * Create or get username.
59
     * @param $username
60
     * @return null|Username
61
     */
62 4
    public function createUsername($username)
63
    {
64 4
        if (!$this->hasEnabledUsername()) {
65
            return null;
66
        }
67 4
        $usernameClass = $this->usernameClass;
68 4
        $model = $usernameClass::findOne($this->getGUID());
0 ignored issues
show
Bug introduced by
It seems like getGUID() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69 4
        if (!$model) {
70 4
            $model = $this->create($usernameClass);
0 ignored issues
show
Bug introduced by
The method create() does not exist on rhosocial\user\models\UserUsernameTrait. Did you maybe mean createUsername()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
71 4
            $model->setGUID($this->getGUID());
0 ignored issues
show
Bug introduced by
It seems like getGUID() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
        }
73 4
        $model->content = $username;
74 4
        return $model;
75
    }
76
77
    /**
78
     * Set username.
79
     * @param string|Username $username
80
     * @return bool
81
     */
82 2
    public function setUsername($username = null)
83
    {
84 2
        if ($username === null && ($model = $this->getUsername()->one())) {
85 1
            return $model->delete() > 0;
86
        }
87 1
        if ($username instanceof Username) {
88
            $username = $username->content;
89
        }
90 1
        $model = $this->createUsername($username);
91 1
        return $model->save();
92
    }
93
94
    /**
95
     * Remove username.
96
     * @return bool
97
     */
98 1
    public function removeUsername()
99
    {
100 1
        return $this->setUsername(null);
101
    }
102
}
103