|
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); |
|
|
|
|
|
|
74
|
|
|
try { |
|
75
|
|
|
return $log->save(); |
|
76
|
|
|
} catch (\Exception $ex) { |
|
77
|
|
|
Yii::error($ex->getMessage(), __METHOD__); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
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
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). 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.