Completed
Push — master ( 1aa4be...d9087d )
by Mihail
02:52
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 = 'guest')
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
    /**
41
     * Prepare HTML DOM link (a href) to user with him name or guest name without link
42
     * @param int $userId
43
     * @param string $onEmpty
44
     * @param string $controllerAction
45
     * @return string
46
     */
47
    public function parseUserLink($userId = null, $onEmpty = 'guest', $controllerAction = 'profile/show')
48
    {
49
        $nick = self::parseUserNick($userId, $onEmpty);
50
        // new name is not found, lets return default
51
        if ($nick === $onEmpty || (int)$userId < 1) {
52
            return $nick;
53
        }
54
55
        return Url::link([$controllerAction, (int)$userId], $nick);
56
    }
57
58
}