CurrentUser::getUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\actions\traits;
10
11
use Craft;
12
use craft\elements\User as UserElement;
13
use yii\web\HttpException;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
trait CurrentUser
20
{
21
    /**
22
     * @var int
23
     */
24
    public $accessCodeUserNotFound = 401;
25
26
    /**
27
     * @return UserElement
28
     */
29
    protected function findUser()
30
    {
31
        return Craft::$app->getUser()->getIdentity();
32
    }
33
34
    /**
35
     * @return UserElement
36
     */
37
    protected function getUser()
38
    {
39
        if (($currentUser = $this->findUser()) === null) {
40
            return $this->handleUserNotFoundResponse();
41
        };
42
43
        return $currentUser;
44
    }
45
46
    /**
47
     * @throws HttpException
48
     * @return mixed
49
     */
50
    protected function handleUserNotFoundResponse()
51
    {
52
        throw new HttpException(
53
            $this->accessCodeUserNotFound,
54
            'Unable to establish identity.'
55
        );
56
    }
57
}
58