Completed
Push — master ( 5bbdfe...13b325 )
by Mihail
04:51
created

Simplify::parseUserNick()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace Ffcms\Core\Helper;
4
5
6
use Ffcms\Core\App;
7
use Ffcms\Core\Helper\Type\Obj;
8
9
/**
10
 * Class Simplify. Simplification of ofter used logic and methods.
11
 * @package Ffcms\Core\Helper
12
 */
13
class Simplify
14
{
15
    /**
16
     * Get user nickname by user id with predefined value on empty or not exist profile
17
     * @param $userId
18
     * @param string $onEmpty
19
     * @return string
20
     */
21
    public static function parseUserNick($userId = null, $onEmpty = 'unknown')
22
    {
23
        // try to get user id as integer
24
        if (Obj::isLikeInt($userId)) {
25
            $userId = (int)$userId;
26
        } else { // user id is empty, lets return default value
27
            return \App::$Security->strip_tags($onEmpty);
28
        }
29
30
        // try to find user active record as object
31
        $identity = App::$User->identity($userId);
32
        if ($identity === null || $identity === false) {
33
            return \App::$Security->strip_tags($onEmpty);
34
        }
35
36
        // return user nickname from profile
37
        return $identity->getProfile()->getNickname();
38
    }
39
40
}