Passed
Push — master ( 998022...62594f )
by Mihail
09:58
created

User::getWall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
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
class User extends ActiveModel implements iUser
12
{
13
14
    /**
15
     * Get user object relation. If $user_id is null - get current session user
16
     * @param int|null $user_id
17
     * @return self|null
18
     */
19
    public static function identity($user_id = null)
20
    {
21
        if ($user_id === null) {
22
            $user_id = MainApp::$Session->get('ff_user_id');
23
        }
24
25
        // convert id to real integer
26
        $user_id = (int)$user_id;
27
28
        if (!Obj::isInt($user_id) || $user_id < 1) {
29
            return null;
30
        }
31
32
        // check in memory cache object
33
        if (MainApp::$Memory->get('user.object.cache.' . $user_id) !== null) {
34
            return MainApp::$Memory->get('user.object.cache.' . $user_id);
35
        }
36
        // not founded in memory? lets make query
37
        $user = self::find($user_id);
38
        // no rows? lets end this shit ;)
39
        if ($user === null || $user->id < 1) {
40
            return null;
41
        }
42
43
        // store cache and return object
44
        MainApp::$Memory->set('user.object.cache.' . $user->id, $user);
45
        return $user;
46
    }
47
48
    /**
49
     * Get current user id if auth
50
     * @return int
51
     */
52
    public function getId()
53
    {
54
        return (int)$this->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Apps\ActiveRecord\User>. 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...
55
    }
56
57
    /**
58
     * Get user param
59
     * @param string $param
60
     * @param null|string $defaultValue
61
     * @return string|int|null
62
     */
63
    public function getParam($param, $defaultValue = null)
64
    {
65
        return $this->{$param} === null ? $defaultValue : $this->{$param};
66
    }
67
68
    /**
69
     * Check if current user session is auth
70
     * @return bool
71
     */
72
    public static function isAuth()
73
    {
74
        // get data from session
75
        $session_token = MainApp::$Session->get('ff_user_token', null);
76
        $session_id = (int)MainApp::$Session->get('ff_user_id', 0);
77
78
        // validate session data
79
        if (null === $session_token || $session_id < 1 || Str::length($session_token) < 64) {
80
            return false;
81
        }
82
83
        // find user identity
84
        $find = self::identity($session_id);
85
        if (null === $find || Str::length($find->token_data) < 64) { // check if this $id exist
0 ignored issues
show
Documentation introduced by
The property token_data does not exist on object<Apps\ActiveRecord\User>. 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...
86
            MainApp::$Session->invalidate(); // destory session data - it's not valid!
87
            return false;
88
        }
89
90
        // check if user is approved. Default value: 0, can be null, '' or the same.
91
        if ($find->approve_token !== '0' && Str::length($find->approve_token) > 0) {
0 ignored issues
show
Documentation introduced by
The property approve_token does not exist on object<Apps\ActiveRecord\User>. 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...
92
            return false;
93
        }
94
95
        return $find->token_data === $session_token;
0 ignored issues
show
Documentation introduced by
The property token_data does not exist on object<Apps\ActiveRecord\User>. 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...
96
    }
97
98
    /**
99
     * Check if user with $id exist
100
     * @param int $id
101
     * @return bool
102
     */
103
    public static function isExist($id)
104
    {
105
        if (!Obj::isLikeInt($id) || $id < 1) {
106
            return false;
107
        }
108
109
        // convert id to real integer
110
        $id = (int)$id;
111
112
        $find = MainApp::$Memory->get('user.counter.cache.' . $id);
113
        if ($find === null) {
114
            $find = self::where('id', '=', $id)->count();
115
            MainApp::$Memory->set('user.counter.cache.' . $id, $find);
116
        }
117
118
        return $find === 1;
119
    }
120
121
    /**
122
     * Check if use with $email is exist
123
     * @param string $email
124
     * @return bool
125
     */
126 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...
127
    {
128
        if (!Obj::isString($email) || !Str::isEmail($email)) {
129
            return false;
130
        }
131
132
        return self::where('email', '=', $email)->count() > 0;
133
    }
134
135
    /**
136
     * Check if user with $login is exist
137
     * @param string $login
138
     * @return bool
139
     */
140 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...
141
    {
142
        if (!Obj::isString($login) || Str::length($login) < 1) {
143
            return false;
144
        }
145
146
        return self::where('login', '=', $login)->count() > 0;
147
    }
148
149
    /**
150
     * Get user person like a object via email
151
     * @param string $email
152
     * @return null|static
153
     */
154
    public static function getIdentityViaEmail($email)
155
    {
156
        if (!self::isMailExist($email)) {
157
            return null;
158
        }
159
160
        return self::where('email', '=', $email)->first();
161
    }
162
163
    /**
164
     * Get relation one-to-many for user wall posts. Ex: User::find(1)->getWall()->offset()
165
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
166
     */
167
    public function getWall()
168
    {
169
        return $this->hasMany('Apps\\ActiveRecord\\WallPost', 'target_id');
170
    }
171
172
    /**
173
     * Get user role object
174
     * @return \Apps\ActiveRecord\Role|null
175
     */
176
    public function getRole()
177
    {
178
        return Role::get($this->role_id);
0 ignored issues
show
Documentation introduced by
The property role_id does not exist on object<Apps\ActiveRecord\User>. 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...
179
    }
180
181
    /**
182
     * Get user profile data as relation of user table. Ex: User::find(1)->getProfile()->nick
183
     * @return \Apps\ActiveRecord\Profile
184
     */
185
    public function getProfile()
186
    {
187
        // lets find profile identity via current user id
188
        $object = Profile::identity($this->getId());
189
        // is not exist? Hmmm, lets create it!
190
        if ($object === null && $this->getId() > 0) {
191
            $object = new Profile();
192
            $object->user_id = $this->getId();
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<Apps\ActiveRecord\Profile>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
193
            $object->save();
194
        }
195
        // 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...
196
        return $object;
197
    }
198
199
    /**
200
     * Check if target user in blacklist
201
     * @param int $target_id
202
     * @return bool
203
     */
204
    public function inBlacklist($target_id)
205
    {
206
        return Blacklist::have($this->getId(), $target_id);
207
    }
208
209
}