Passed
Push — master ( ef8807...c1ca83 )
by Mihail
05:17
created

Profile::user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
123
    }
124
}
125