Passed
Push — master ( 6084dc...68443e )
by Mihail
05:40
created

Profile::identity()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 28
rs 4.909
cc 9
eloc 13
nc 8
nop 1
1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\Arch\ActiveModel;
6
use Ffcms\Core\Helper\Type\Arr;
7
use Ffcms\Core\Helper\FileSystem\File;
8
use Ffcms\Core\Helper\Type\Obj;
9
use Ffcms\Core\Helper\Type\Str;
10
use Ffcms\Core\Interfaces\iProfile;
11
use Ffcms\Core\App as MainApp;
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 string $custom_data
27
 * @property string $created_at
28
 * @property string $updated_at
29
 */
30
class Profile extends ActiveModel implements iProfile
31
{
32
33
    /**
34
     * Get user profile via user_id like object (!!! profile.id !== user.id !!!)
35
     * @param int|null $user_id
36
     * @return self|null
37
     */
38
    public static function identity($user_id = null)
39
    {
40
        if ($user_id === null) {
41
            $user_id = MainApp::$Session->get('ff_user_id');
42
        }
43
44
        if ($user_id === null || !Obj::isLikeInt($user_id) || $user_id < 1) {
45
            return null;
46
        }
47
48
        // check in cache
49
        if (MainApp::$Memory->get('profile.object.cache.' . $user_id) !== null) {
50
            return MainApp::$Memory->get('profile.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\iProfile::identity of type Ffcms\Core\Interfaces\iProfile|null.

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...
51
        }
52
53
        // find row
54
        $profile = self::where('user_id', '=', $user_id);
55
56
        // 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...
57
        if (false === $profile || null === $profile || $profile->count() !== 1) {
58
            return null;
59
        }
60
61
        $object = $profile->first();
62
63
        MainApp::$Memory->set('profile.object.cache.' . $user_id, $object);
64
        return $object;
65
    }
66
67
    /**
68
     * Get user avatar full url for current object
69
     * @param string $type
70
     * @return string
71
     */
72
    public function getAvatarUrl($type = 'small')
73
    {
74
        $default = '/upload/user/avatar/' . $type . '/default.jpg';
75
        if (!Arr::in($type, ['small', 'big', 'medium'])) {
76
            return MainApp::$Alias->scriptUrl . $default;
77
        }
78
79
        $route = '/upload/user/avatar/' . $type . '/' . $this->user_id . '.jpg';
80
        if (File::exist($route)) {
81
            return MainApp::$Alias->scriptUrl . $route . '?mtime=' . File::mTime($route);
82
        }
83
84
        return MainApp::$Alias->scriptUrl . $default;
85
    }
86
87
    /**
88
     * Get user nickname. If is empty - return 'id+userId'
89
     * @return string
90
     */
91
    public function getNickname()
92
    {
93
        $userNick = $this->nick;
94
        if ($userNick === null || Str::likeEmpty($userNick)) {
95
            $userNick = 'id' . $this->id;
96
        }
97
98
        return $userNick;
99
    }
100
101
    /**
102
     * Get user identity for current object
103
     * @return User|null
104
     */
105
    public function User()
106
    {
107
        return User::identity($this->user_id);
108
    }
109
}