Passed
Push — master ( f22cf7...1cd2c2 )
by Mihail
04:42
created

User::getLogs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\Arch\ActiveModel;
6
use Ffcms\Core\Interfaces\iUser;
7
use Ffcms\Core\App as MainApp;
8
use Ffcms\Core\Helper\Type\Obj;
9
use Ffcms\Core\Helper\Type\Str;
10
11
/**
12
 * Class User. Active record model for user auth data
13
 * @package Apps\ActiveRecord
14
 * @property int $id
15
 * @property string $login
16
 * @property string $email
17
 * @property string $password
18
 * @property int $role_id
19
 * @property string $approve_token
20
 * @property string $created_at
21
 * @property string $updated_at
22
 */
23
class User extends ActiveModel implements iUser
24
{
25
26
    /**
27
     * Get user object relation. If $user_id is null - get current session user
28
     * @param int|null $user_id
29
     * @return self|null
30
     */
31
    public static function identity($user_id = null)
32
    {
33
        if ($user_id === null) {
34
            $user_id = MainApp::$Session->get('ff_user_id');
35
        }
36
37
        // convert id to real integer
38
        $user_id = (int)$user_id;
39
40
        if (!Obj::isInt($user_id) || $user_id < 1) {
41
            return null;
42
        }
43
44
        // check in memory cache object
45
        if (MainApp::$Memory->get('user.object.cache.' . $user_id) !== null) {
46
            return MainApp::$Memory->get('user.object.cache.' . $user_id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Ffcms\Core\App::...ct.cache.' . $user_id); (object|array|string|boolean) is incompatible with the return type declared by the interface Ffcms\Core\Interfaces\iUser::identity of type null|Ffcms\Core\Interfaces\iUser.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
47
        }
48
        // not founded in memory? lets make query
49
        $user = self::find($user_id);
50
        // no rows? lets end this shit ;)
51
        if ($user === null || $user->id < 1) {
52
            return null;
53
        }
54
55
        // store cache and return object
56
        MainApp::$Memory->set('user.object.cache.' . $user->id, $user);
57
        return $user;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $user; (Ffcms\Core\Arch\ActiveModel) is incompatible with the return type declared by the interface Ffcms\Core\Interfaces\iUser::identity of type null|Ffcms\Core\Interfaces\iUser.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
58
    }
59
60
    /**
61
     * Get current user id if auth
62
     * @return int
63
     */
64
    public function getId()
65
    {
66
        return (int)$this->id;
67
    }
68
69
    /**
70
     * Get user param
71
     * @param string $param
72
     * @param null|string $defaultValue
73
     * @return string|int|null
74
     */
75
    public function getParam($param, $defaultValue = null)
76
    {
77
        return $this->{$param} === null ? $defaultValue : $this->{$param};
78
    }
79
80
    /**
81
     * Check if current user session is auth
82
     * @return bool
83
     */
84
    public static function isAuth()
85
    {
86
        // get data from session
87
        $sessionUserId = (int)MainApp::$Session->get('ff_user_id', 0);
88
89
        // check if session contains user id data
90
        if ($sessionUserId < 1) {
91
            return false;
92
        }
93
94
        // find user identity
95
        $identity = self::identity($sessionUserId);
96
        if ($identity === null) { // check if this $id exist
97
            MainApp::$Session->invalidate(); // destory session data - it's not valid!
98
            return false;
99
        }
100
101
        // check if user is approved. Default value: 0, can be null, '' or the same.
102
        if ($identity->approve_token !== '0' && Str::length($identity->approve_token) > 0) {
103
            return false;
104
        }
105
106
        return ((int)$identity->id > 0 && (int)$identity->id === $sessionUserId);
107
    }
108
109
    /**
110
     * Check if user with $id exist
111
     * @param int $id
112
     * @return bool
113
     */
114
    public static function isExist($id)
115
    {
116
        if (!Obj::isLikeInt($id) || $id < 1) {
117
            return false;
118
        }
119
120
        // convert id to real integer
121
        $id = (int)$id;
122
123
        $find = MainApp::$Memory->get('user.counter.cache.' . $id);
124
        if ($find === null) {
125
            $find = self::where('id', '=', $id)->count();
126
            MainApp::$Memory->set('user.counter.cache.' . $id, $find);
127
        }
128
129
        return $find === 1;
130
    }
131
132
    /**
133
     * Check if use with $email is exist
134
     * @param string $email
135
     * @return bool
136
     */
137 View Code Duplication
    public static function isMailExist($email)
0 ignored issues
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...
138
    {
139
        if (!Obj::isString($email) || !Str::isEmail($email)) {
140
            return false;
141
        }
142
143
        return self::where('email', '=', $email)->count() > 0;
144
    }
145
146
    /**
147
     * Check if user with $login is exist
148
     * @param string $login
149
     * @return bool
150
     */
151 View Code Duplication
    public static function isLoginExist($login)
0 ignored issues
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...
152
    {
153
        if (!Obj::isString($login) || Str::length($login) < 1) {
154
            return false;
155
        }
156
157
        return self::where('login', '=', $login)->count() > 0;
158
    }
159
160
    /**
161
     * Get user person like a object via email
162
     * @param string $email
163
     * @return null|static
164
     */
165
    public static function getIdentityViaEmail($email)
166
    {
167
        if (!self::isMailExist($email)) {
168
            return null;
169
        }
170
171
        return self::where('email', '=', $email)->first();
0 ignored issues
show
Bug Compatibility introduced by
The expression self::where('email', '=', $email)->first(); of type Ffcms\Core\Arch\ActiveModel|null adds the type Ffcms\Core\Arch\ActiveModel to the return on line 171 which is incompatible with the return type declared by the interface Ffcms\Core\Interfaces\iUser::getIdentityViaEmail of type Ffcms\Core\Interfaces\iUser|null.
Loading history...
172
    }
173
174
    /**
175
     * Get relation one-to-many for user wall posts. Ex: User::find(1)->getWall()->offset()
176
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
177
     */
178
    public function getWall()
179
    {
180
        return $this->hasMany('Apps\\ActiveRecord\\WallPost', 'target_id');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->hasMany('A...allPost', 'target_id'); (Illuminate\Database\Eloquent\Relations\HasMany) is incompatible with the return type declared by the interface Ffcms\Core\Interfaces\iUser::getWall of type Apps\ActiveRecord\WallPost.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
181
    }
182
183
    /**
184
     * Get user role object
185
     * @return \Apps\ActiveRecord\Role|null
186
     */
187
    public function getRole()
188
    {
189
        return Role::get($this->role_id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Apps\ActiveRecor...e::get($this->role_id); (null|object|array|string|boolean) is incompatible with the return type declared by the interface Ffcms\Core\Interfaces\iUser::getRole of type Apps\ActiveRecord\Role.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
190
    }
191
192
    /**
193
     * Get user profile data as relation of user table. Ex: User::find(1)->getProfile()->nick
194
     * @return \Apps\ActiveRecord\Profile
195
     */
196
    public function getProfile()
197
    {
198
        // lets find profile identity via current user id
199
        $object = Profile::identity($this->getId());
200
        // is not exist? Hmmm, lets create it!
201
        if ($object === null && $this->getId() > 0) {
202
            $object = new Profile();
203
            $object->user_id = $this->getId();
204
            $object->save();
205
        }
206
        // return result ;)
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
207
        return $object;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $object; (object|array|string|boolean|null) is incompatible with the return type declared by the interface Ffcms\Core\Interfaces\iUser::getProfile of type Apps\ActiveRecord\Profile.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
208
    }
209
210
    /**
211
     * Get user logs
212
     * @return \Apps\ActiveRecord\UserLog
213
     */
214
    public function getLogs()
215
    {
216
        return $this->hasMany('Apps\\ActiveRecord\\UserLog', 'user_id');
217
    }
218
219
    /**
220
     * Check if target user in blacklist
221
     * @param int $target_id
222
     * @return bool
223
     */
224
    public function inBlacklist($target_id)
225
    {
226
        return Blacklist::have($this->getId(), $target_id);
227
    }
228
229
}