Completed
Pull Request — master (#23)
by Evan
02:46
created

Model::fromSlug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 8
loc 8
rs 9.4285
1
<?php
2
3
namespace Silk\User;
4
5
use WP_User;
6
use Silk\Type\Model as BaseModel;
7
use Silk\Exception\WP_ErrorException;
8
use Silk\User\Exception\UserNotFoundException;
9
10
class Model extends BaseModel
11
{
12
    /**
13
     * The object type in WordPress
14
     */
15
    const OBJECT_TYPE = 'user';
16
17
    /**
18
     * The primary ID property on the object
19
     */
20
    const ID_PROPERTY = 'ID';
21
22
    /**
23
     * Type object property aliases
24
     * @var array
25
     */
26
    protected $objectAliases = [
27
        'email'    => 'user_email',
28
        'slug'     => 'user_nicename',
29
        'username' => 'user_login',
30
        'password' => 'user_pass',
31
    ];
32
33
    /**
34
     * User Constructor.
35
     *
36
     * @param WP_User $user
37
     */
38
    public function __construct(WP_User $user = null)
39
    {
40
        if (! $user) {
41
            $user = new WP_User;
42
        }
43
44
        $this->object = $user;
45
    }
46
47
    /**
48
     * Create a new instance from the user ID.
49
     *
50
     * @param  string|int $id  User ID
51
     *
52
     * @throws UserNotFoundException
53
     *
54
     * @return static
55
     */
56 View Code Duplication
    public static function fromID($id)
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...
57
    {
58
        if (! $user = get_user_by('id', $id)) {
59
            throw new UserNotFoundException("No user found with ID $id");
60
        }
61
62
        return new static($user);
63
    }
64
65
    /**
66
     * Create a new instance from the username.
67
     *
68
     * @param  string $username  Username (login)
69
     *
70
     * @throws UserNotFoundException
71
     *
72
     * @return static
73
     */
74
    public static function fromUsername($username)
75
    {
76
        if (! $user = get_user_by('login', $username)) {
77
            throw new UserNotFoundException("No user found with username: $username");
78
        }
79
80
        return new static($user);
81
    }
82
83
    /**
84
     * Create a new instance from the user's email address.
85
     *
86
     * @param  string $email  User email address
87
     *
88
     * @throws UserNotFoundException
89
     *
90
     * @return static
91
     */
92 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...
93
    {
94
        if (! $user = get_user_by('email', $email)) {
95
            throw new UserNotFoundException("No user found with email address: $email");
96
        }
97
98
        return new static($user);
99
    }
100
101
    /**
102
     * Create a new instance from the user's slug.
103
     *
104
     * @param  string $slug  User slug (nicename)
105
     *
106
     * @throws UserNotFoundException
107
     *
108
     * @return static
109
     */
110 View Code Duplication
    public static function fromSlug($slug)
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...
111
    {
112
        if (! $user = get_user_by('slug', $slug)) {
113
            throw new UserNotFoundException("No user found with slug: $slug");
114
        }
115
116
        return new static($user);
117
    }
118
119
    /**
120
     * Create a new instance using the currently authenticated user.
121
     *
122
     * @return static
123
     */
124
    public static function auth()
125
    {
126
        return new static(wp_get_current_user());
127
    }
128
129
    /**
130
     * Get the URL for the user's posts archive.
131
     *
132
     * @return string
133
     */
134
    public function postsUrl()
135
    {
136
        return get_author_posts_url($this->id, $this->slug);
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<Silk\User\Model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
137
    }
138
139
    /**
140
    * Get a new query builder for the model.
141
    *
142
    * @return \Silk\Contracts\BuildsQueries
143
    */
144
    public function newQuery()
145
    {
146
        return QueryBuilder::make()->setModel($this);
147
    }
148
149
    /**
150
     * Save the changes to the database.
151
     *
152
     * @throws WP_ErrorException
153
     *
154
     * @return $this
155
     */
156 View Code Duplication
    public function save()
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...
157
    {
158
        if (! $this->id) {
159
            $result = wp_insert_user($this->object);
160
        } else {
161
            $result = wp_update_user($this->object);
162
        }
163
164
        if (is_wp_error($result)) {
165
            throw new WP_ErrorException($result);
166
        }
167
168
        $this->setId($result)->refresh();
169
170
        return $this;
171
    }
172
173
    /**
174
     * Delete the modeled record from the database.
175
     *
176
     * @return $this
177
     */
178
    public function delete()
179
    {
180
        if (wp_delete_user($this->id)) {
181
            $this->object = new WP_User;
182
        }
183
184
        return $this;
185
    }
186
187
    /**
188
     * Reload the object from the database.
189
     *
190
     * @return $this
191
     */
192
    public function refresh()
193
    {
194
        $this->object = new WP_User($this->id);
195
196
        return $this;
197
    }
198
}
199