Completed
Pull Request — master (#23)
by Evan
05:41 queued 02:40
created

Model::save()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 0
dl 16
loc 16
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 array|WP_User $user  User object or array of attributes
37
     */
38
    public function __construct($user = null)
39
    {
40
        $attributes = is_array($user) ? $user : [];
41
42
        if (! $user instanceof WP_User) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
43
            $user = new WP_User();
44
        }
45
46
        $this->object = $user;
47
48
        $this->fill($attributes);
49
    }
50
51
    /**
52
     * Create a new instance from the user ID.
53
     *
54
     * @param  string|int $id  User ID
55
     *
56
     * @throws UserNotFoundException
57
     *
58
     * @return static
59
     */
60 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...
61
    {
62
        if (! $user = get_user_by('id', $id)) {
63
            throw new UserNotFoundException("No user found with ID $id");
64
        }
65
66
        return new static($user);
67
    }
68
69
    /**
70
     * Create a new instance from the username.
71
     *
72
     * @param  string $username  Username (login)
73
     *
74
     * @throws UserNotFoundException
75
     *
76
     * @return static
77
     */
78
    public static function fromUsername($username)
79
    {
80
        if (! $user = get_user_by('login', $username)) {
81
            throw new UserNotFoundException("No user found with username: $username");
82
        }
83
84
        return new static($user);
85
    }
86
87
    /**
88
     * Create a new instance from the user's email address.
89
     *
90
     * @param  string $email  User email address
91
     *
92
     * @throws UserNotFoundException
93
     *
94
     * @return static
95
     */
96 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...
97
    {
98
        if (! $user = get_user_by('email', $email)) {
99
            throw new UserNotFoundException("No user found with email address: $email");
100
        }
101
102
        return new static($user);
103
    }
104
105
    /**
106
     * Create a new instance from the user's slug.
107
     *
108
     * @param  string $slug  User slug (nicename)
109
     *
110
     * @throws UserNotFoundException
111
     *
112
     * @return static
113
     */
114 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...
115
    {
116
        if (! $user = get_user_by('slug', $slug)) {
117
            throw new UserNotFoundException("No user found with slug: $slug");
118
        }
119
120
        return new static($user);
121
    }
122
123
    /**
124
     * Create a new instance using the currently authenticated user.
125
     *
126
     * @return static
127
     */
128
    public static function auth()
129
    {
130
        return new static(wp_get_current_user());
131
    }
132
133
    /**
134
     * Get the URL for the user's posts archive.
135
     *
136
     * @return string
137
     */
138
    public function postsUrl()
139
    {
140
        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...
141
    }
142
143
    /**
144
    * Get a new query builder for the model.
145
    *
146
    * @return \Silk\Contracts\BuildsQueries
147
    */
148
    public function newQuery()
149
    {
150
        return QueryBuilder::make()->setModel($this);
151
    }
152
153
    /**
154
     * Save the changes to the database.
155
     *
156
     * @throws WP_ErrorException
157
     *
158
     * @return $this
159
     */
160 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...
161
    {
162
        if (! $this->id) {
163
            $result = wp_insert_user($this->object);
164
        } else {
165
            $result = wp_update_user($this->object);
166
        }
167
168
        if (is_wp_error($result)) {
169
            throw new WP_ErrorException($result);
170
        }
171
172
        $this->setId($result)->refresh();
173
174
        return $this;
175
    }
176
177
    /**
178
     * Delete the modeled record from the database.
179
     *
180
     * @return $this
181
     */
182
    public function delete()
183
    {
184
        if (wp_delete_user($this->id)) {
185
            $this->object = new WP_User;
186
        }
187
188
        return $this;
189
    }
190
191
    /**
192
     * Reload the object from the database.
193
     *
194
     * @return $this
195
     */
196
    public function refresh()
197
    {
198
        $this->object = new WP_User($this->id);
199
200
        return $this;
201
    }
202
}
203