Completed
Branch develop (2a5993)
by Evan
02:52
created

Model::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
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
    public static function fromSlug($slug)
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
     * Get the URL for the user's posts archive.
121
     *
122
     * @return string
123
     */
124
    public function postsUrl()
125
    {
126
        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...
127
    }
128
129
    /**
130
    * Get a new query builder for the model.
131
    *
132
    * @return \Silk\Contracts\BuildsQueries
133
    */
134
    public function newQuery()
135
    {
136
        return QueryBuilder::make();
137
    }
138
139
140
    /**
141
     * Save the changes to the database.
142
     *
143
     * @throws WP_ErrorException
144
     *
145
     * @return $this
146
     */
147
    public function save()
148
    {
149
        if (! $this->id) {
150
            $result = wp_insert_user($this->object);
151
        } else {
152
            $result = wp_update_user($this->object);
153
        }
154
155
        if (is_wp_error($result)) {
156
            throw new WP_ErrorException($result);
157
        }
158
159
        $this->setId($result);
160
161
        return $this;
162
    }
163
164
    /**
165
     * Delete the modeled record from the database.
166
     *
167
     * @return $this
168
     */
169
    public function delete()
170
    {
171
        if (wp_delete_user($this->id)) {
172
            $this->object = new WP_User;
173
        }
174
175
        return $this;
176
    }
177
178
    /**
179
     * Reload the object from the database.
180
     *
181
     * @return $this
182
     */
183
    public function refresh()
184
    {
185
        $this->object = new WP_User($this->id);
186
187
        return $this;
188
    }
189
}
190