Completed
Push — develop ( 4c8faa...5d0cc3 )
by Evan
03:17
created

Model::typeId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Silk\User;
4
5
use WP_User;
0 ignored issues
show
introduced by
Use classes must be in alphabetical order.
Loading history...
6
use Silk\Type\Model as BaseModel;
7
use Silk\User\Exception\UserNotFoundException;
8
9
class Model extends BaseModel
10
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
11
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
12
     * The object type in WordPress
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
13
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
14
    const OBJECT_TYPE = 'user';
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
15
16
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
17
     * The primary ID property on the object
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
18
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
19
    const ID_PROPERTY = 'ID';
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
20
21
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
22
     * Type object property aliases
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
23
     * @var array
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
24
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
25
    protected $objectAliases = [
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
26
        'email'    => 'user_email',
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Double space found
Loading history...
27
        'slug'     => 'user_nicename',
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Double space found
Loading history...
28
        'username' => 'user_login',
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
29
        'password' => 'user_pass',
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
30
    ];
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
31
32
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
33
     * User Constructor.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
34
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
35
     * @param WP_User $user
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Possible doc block error: WP_User seems to be missing type null.
Loading history...
introduced by
WP_User => \WP_User
Loading history...
36
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
37
    public function __construct(WP_User $user = null)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
38
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
39
        if (! $user) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
40
            $user = new WP_User;
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Calling class constructors must always include parentheses
Loading history...
41
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
42
43
        $this->object = $user;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
44
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
45
46
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
47
     * Create a new instance from the user ID.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
48
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
49
     * @param  string|int $id  User ID
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
50
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
51
     * @throws UserNotFoundException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
UserNotFoundException => \Silk\User\Exception\UserNotFoundException
Loading history...
52
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
53
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
54
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
55 View Code Duplication
    public static function fromID($id)
1 ignored issue
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
57
        if (! $user = get_user_by('id', $id)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Conditional inline assignment not allowed
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
58
            throw new UserNotFoundException("No user found with ID $id");
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
59
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
60
61
        return new static($user);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
62
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
63
64
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
65
     * Create a new instance from the username.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
66
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
67
     * @param  string $username  Username (login)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
68
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
69
     * @throws UserNotFoundException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
UserNotFoundException => \Silk\User\Exception\UserNotFoundException
Loading history...
70
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
71
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
72
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
73
    public static function fromUsername($username)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
74
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
75
        if (! $user = get_user_by('login', $username)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Conditional inline assignment not allowed
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
76
            throw new UserNotFoundException("No user found with username: $username");
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
77
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
78
79
        return new static($user);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
80
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
81
82
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
83
     * Create a new instance from the user's email address.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
84
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
85
     * @param  string $email  User email address
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
86
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
87
     * @throws UserNotFoundException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
UserNotFoundException => \Silk\User\Exception\UserNotFoundException
Loading history...
88
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
89
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
90
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
91 View Code Duplication
    public static function fromEmail($email)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
introduced by
4 spaces found, expected 1 tabs
Loading history...
92
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
93
        if (! $user = get_user_by('email', $email)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Conditional inline assignment not allowed
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
94
            throw new UserNotFoundException("No user found with email address: $email");
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
95
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
96
97
        return new static($user);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
98
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
99
100
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
101
     * Create a new instance from the user's slug.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
102
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
103
     * @param  string $slug  User slug (nicename)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
104
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
105
     * @throws UserNotFoundException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
UserNotFoundException => \Silk\User\Exception\UserNotFoundException
Loading history...
106
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
107
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
108
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
109
    public static function fromSlug($slug)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
110
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
111
        if (! $user = get_user_by('slug', $slug)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Conditional inline assignment not allowed
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
112
            throw new UserNotFoundException("No user found with slug: $slug");
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
113
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
114
115
        return new static($user);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
116
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
117
118
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
119
    * Get a new query builder for the model.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
120
    *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
121
    * @return \Silk\Contracts\BuildsQueries
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
122
    */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
123
    public function newQuery()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
124
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
125
        return QueryBuilder::make();
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
126
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
127
128
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
129
     * Get the map of action => class for resolving active actions.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
130
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
131
     * @return array
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
132
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
133
    protected function actionClasses()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
134
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
135
        return [
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
136
            'save' => Action\UserSaver::class,
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Use statement Action\UserSaver for UserSaver should be in use block.
Loading history...
137
            'delete' => Action\UserDeleter::class,
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Use statement Action\UserDeleter for UserDeleter should be in use block.
Loading history...
138
        ];
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
139
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
140
141
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
142
     * Magic getter.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
143
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
144
     * @param  string $property
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
145
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
146
     * @return mixed
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
147
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
148
    public function __get($property)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
149
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
150
        if (! array_key_exists($property, $this->objectAliases)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
151
            return parent::__get($property);
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
152
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
153
154
        return data_get($this->object, $this->objectAliases[$property]);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
155
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
156
157
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
158
     * Magic setter.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
159
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
160
     * @param string $property  The property name
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
161
     * @param mixed  $value     The new property value
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
162
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
163
    public function __set($property, $value)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Method does not have a return void statement in doc block: __set
Loading history...
164
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
165
        if (array_key_exists($property, $this->objectAliases)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
166
            $property = $this->objectAliases[$property];
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
167
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
168
169
        $this->object->$property = $value;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
170
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
171
}
0 ignored issues
show
introduced by
Closing brace of a class must have a new line between itself and the last content.
Loading history...
172