UserLoginTrait::recordLogin()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 0
cts 8
cp 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\models\log;
14
15
use rhosocial\user\User;
16
use Yii;
17
18
/**
19
 * This trait provides login log access methods.
20
 *
21
 * @property-read Login[] $loginLogs
22
 * @property-read Login $latestLoginLog
23
 *
24
 * @version 1.0
25
 * @author vistart <[email protected]>
26
 */
27
trait UserLoginTrait
28
{
29
    public $loginLogClass = Login::class;
30
    
31
    /**
32
     * Get login logs.
33
     * @return Login[]
34
     */
35
    public function getLoginLogs()
36
    {
37
        /* @var $this User */
38
        $class = $this->loginLogClass;
39
        try {
40
            return $class::getLatests($this, Login::GET_ALL_LATESTS);
41
        } catch (\Exception $ex) {
42
            Yii::error($ex->getMessage(), __METHOD__);
43
            return [];
44
        }
45
    }
46
    
47
    /**
48
     * Get latest login log.
49
     * @return Login
50
     */
51
    public function getLatestLoginLog()
52
    {
53
        /* @var $this User */
54
        $class = $this->loginLogClass;
55
        try {
56
            return $class::getLatests($this, 1)[0];
57
        } catch (\Exception $ex) {
58
            Yii::error($ex->getMessage(), __METHOD__);
59
            return null;
60
        }
61
    }
62
63
    /**
64
     * Record login.
65
     * @param array $config
66
     * @return mixed
67
     */
68
    public function recordLogin($config = [])
69
    {
70
        if (empty($this->loginLogClass)) {
71
            return false;
72
        }
73
        $log = $this->create($this->loginLogClass, $config);
0 ignored issues
show
Bug introduced by
It seems like create() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
        try {
75
            return $log->save();
76
        } catch (\Exception $ex) {
77
            Yii::error($ex->getMessage(), __METHOD__);
78
        }
79
    }
80
}
81