1
|
|
|
<?php |
2
|
|
|
namespace App\Event; |
3
|
|
|
|
4
|
|
|
use Cake\Cache\Cache; |
5
|
|
|
use Cake\Event\Event; |
6
|
|
|
use Cake\Event\EventListenerInterface; |
7
|
|
|
use Cake\I18n\Number; |
8
|
|
|
use Cake\ORM\TableRegistry; |
9
|
|
|
|
10
|
|
|
class Statistics implements EventListenerInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ImplementedEvents method. |
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
|
|
public function implementedEvents() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
'Model.Users.register' => 'newUserStats', |
22
|
|
|
'Model.Groups.update' => 'updateGroupStats' |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Re-count the number of user and find the latest user and write it in the Cache. |
28
|
|
|
* |
29
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
30
|
|
|
* |
31
|
|
|
* @return array|false |
32
|
|
|
*/ |
33
|
|
|
public function newUserStats(Event $event) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->Users = TableRegistry::get('Users'); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$totalUsers = $this->Users->find()->count(); |
38
|
|
|
$totalUsers = Number::format($totalUsers); |
39
|
|
|
|
40
|
|
|
$lastRegistered = $this->Users->find('short')->order(['Users.created' => 'DESC'])->first(); |
41
|
|
|
|
42
|
|
|
$data = []; |
43
|
|
|
$data['TotalUsers'] = $totalUsers; |
44
|
|
|
$data['LastRegistered'] = $lastRegistered; |
45
|
|
|
|
46
|
|
|
if ($this->_writeCache($data, 'Users')) { |
|
|
|
|
47
|
|
|
return $data; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the Groups and write it in the Cache. |
55
|
|
|
* |
56
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
57
|
|
|
* |
58
|
|
|
* @return array|false |
59
|
|
|
*/ |
60
|
|
|
public function updateGroupStats(Event $event) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$this->Groups = TableRegistry::get('Groups'); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$groups = $this->Groups->find('translations')->order(['Groups.id' => 'DESC'])->toArray(); |
65
|
|
|
|
66
|
|
|
if ($this->_writeCache($groups, 'Groups')) { |
|
|
|
|
67
|
|
|
return $groups; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Write the data into the Cache with the passed key. |
75
|
|
|
* |
76
|
|
|
* @param int|object|string $data The data to save in the Cache. |
77
|
|
|
* @param string $key The key to save the data. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
protected function _writeCache($data, $key) |
82
|
|
|
{ |
83
|
|
|
if (empty($data) || empty($key)) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$result = Cache::write($key, $data, 'statistics'); |
88
|
|
|
if ($result) { |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.