User   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 53
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 18 4
A get() 0 14 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organization\helpers;
10
11
use Craft;
12
use craft\elements\User as UserElement;
13
use yii\base\ErrorException as Exception;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
class User
20
{
21
22
    /*******************************************
23
     * FIND / GET USER ELEMENT
24
     *******************************************/
25
26
    /**
27
     * Find a user element based on id, username or email
28
     *
29
     * @param $user
30
     * @return UserElement|null
31
     */
32
    public static function resolve($user = 'CURRENT_USER')
33
    {
34
35
        if ($user instanceof UserElement) {
36
            return $user;
37
38
            // Current
39
        } elseif ('CURRENT_USER' === $user) {
40
            return Craft::$app->getUser()->getIdentity();
41
42
            // Model
43
        } elseif (is_numeric($user)) {
44
            return Craft::$app->getUsers()->getUserById($user);
45
        }
46
47
        // String
48
        return Craft::$app->getUsers()->getUserByUsernameOrEmail($user);
49
    }
50
51
    /**
52
     * Get a user element based on id, username or email
53
     * @param $user
54
     * @return UserElement|null
55
     * @throws Exception
56
     */
57
    public static function get($user = 'CURRENT_USER')
58
    {
59
        $userElement = static::resolve($user);
60
61
        if (is_null($userElement)) {
62
            throw new Exception(Craft::t(
63
                'organization',
64
                'No user exists with the attribute "{attribute}".',
65
                ['attribute' => $user]
66
            ));
67
        }
68
69
        return $userElement;
70
    }
71
}
72