Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/ActiveRecord/Profile.php (1 issue)

Checks if the types of returned expressions are compatible with the documented types.

Best Practice Bug Major
1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\App as MainApp;
6
use Ffcms\Core\Arch\ActiveModel;
7
use Ffcms\Core\Helper\FileSystem\File;
8
use Ffcms\Core\Helper\Type\Any;
9
use Ffcms\Core\Helper\Type\Arr;
10
use Ffcms\Core\Helper\Type\Str;
11
use Ffcms\Core\Interfaces\iProfile;
12
13
/**
14
 * Class Profile. Active record model for user profile data store
15
 * @package Apps\ActiveRecord
16
 * @property int $id
17
 * @property int $user_id
18
 * @property string $nick
19
 * @property int $sex
20
 * @property string $birthday
21
 * @property string $city
22
 * @property string $hobby
23
 * @property int $rating
24
 * @property string $phone
25
 * @property string $url
26
 * @property array $custom_data
27
 * @property string $created_at
28
 * @property string $updated_at
29
 * @property User $user
30
 */
31
class Profile extends ActiveModel implements iProfile
32
{
33
    protected $casts = [
34
        'id' => 'integer',
35
        'user_id' => 'integer',
36
        'nick' => 'string',
37
        'sex' => 'integer', // tinyInteger 0|1|2
38
        'birthday' => 'string',
39
        'city' => 'string',
40
        'hobby' => 'string',
41
        'rating' => 'integer',
42
        'phone' => 'string',
43
        'url' => 'string',
44
        'custom_data' => 'serialize'
45
    ];
46
47
    /**
48
     * Get user profile via user_id like object (!!! profile.id !== user.id !!!)
49
     * @param int|null $userId
50
     * @return self|null
51
     */
52
    public static function identity($userId = null)
53
    {
54
        if ($userId === null) {
55
            $userId = MainApp::$Session->get('ff_user_id');
56
        }
57
58
        if ($userId === null || !Any::isInt($userId) || $userId < 1) {
59
            return null;
60
        }
61
62
        // check in cache
63
        if (MainApp::$Memory->get('profile.object.cache.' . $userId) !== null) {
64
            return MainApp::$Memory->get('profile.object.cache.' . $userId);
65
        }
66
67
        // find row
68
        $profile = self::where('user_id', $userId);
69
70
        // empty? lets return null
71
        if ($profile->count() !== 1) {
72
            return null;
73
        }
74
75
        $object = $profile->first();
76
        MainApp::$Memory->set('profile.object.cache.' . $userId, $object);
77
78
        return $object;
0 ignored issues
show
Bug Best Practice introduced by Mihail
The expression return $object also could return the type Ffcms\Core\Arch\ActiveModel which is incompatible with the return type mandated by Ffcms\Core\Interfaces\iProfile::identity() of Ffcms\Core\Interfaces\iProfile|null.
Loading history...
79
    }
80
81
    /**
82
     * Get user avatar full url for current object
83
     * @param string $type
84
     * @return string
85
     */
86
    public function getAvatarUrl($type = 'small')
87
    {
88
        $default = '/upload/user/avatar/' . $type . '/default.jpg';
89
        if (!Arr::in($type, ['small', 'big', 'medium'])) {
90
            return MainApp::$Alias->scriptUrl . $default;
91
        }
92
93
        $route = '/upload/user/avatar/' . $type . '/' . $this->user_id . '.jpg';
94
        if (File::exist($route)) {
95
            return MainApp::$Alias->scriptUrl . $route . '?mtime=' . File::mTime($route);
96
        }
97
98
        return MainApp::$Alias->scriptUrl . $default;
99
    }
100
101
    /**
102
     * Get user nickname. If is empty - return 'id+userId'
103
     * @return string
104
     */
105
    public function getNickname(): ?string
106
    {
107
        $userNick = $this->nick;
108
        if (!$userNick || Str::likeEmpty($userNick)) {
109
            $userNick = 'id' . $this->id;
110
        }
111
112
        return $userNick;
113
    }
114
115
    /**
116
     * Get user active record object relation
117
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
118
     */
119
    public function user()
120
    {
121
        return $this->belongsTo(User::class, 'user_id');
122
    }
123
}
124