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