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
|
|
|
'Model.BlogArticles.new' => 'newArticleStats', |
24
|
|
|
'Model.BlogArticlesLikes.new' => 'newArticleLikeStats', |
25
|
|
|
'Model.BlogArticlesComments.new' => 'newArticleCommentStats' |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Count the articles and write it in the Cache. |
31
|
|
|
* |
32
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
33
|
|
|
* |
34
|
|
|
* @return array|false |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function newArticleStats(Event $event) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$this->BlogArticles = TableRegistry::get('BlogArticles'); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$articles = $this->BlogArticles->find()->count(); |
41
|
|
|
$articles = Number::format($articles); |
42
|
|
|
|
43
|
|
|
if ($this->_writeCache($articles, 'Articles')) { |
44
|
|
|
return $articles; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Count the article's comments and write it in the Cache. |
52
|
|
|
* |
53
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
54
|
|
|
* |
55
|
|
|
* @return array|false |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function newArticleCommentStats(Event $event) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->BlogArticlesComments = TableRegistry::get('BlogArticlesComments'); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$comments = $this->BlogArticlesComments->find()->count(); |
|
|
|
|
62
|
|
|
$comments = Number::format($comments); |
63
|
|
|
|
64
|
|
|
if ($this->_writeCache($comments, 'ArticlesComments')) { |
65
|
|
|
return $comments; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Count the article's likes and write it in the Cache. |
73
|
|
|
* |
74
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
75
|
|
|
* |
76
|
|
|
* @return array|false |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function newArticleLikeStats(Event $event) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->BlogArticlesLikes = TableRegistry::get('BlogArticlesLikes'); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$likes = $this->BlogArticlesLikes->find()->count(); |
|
|
|
|
83
|
|
|
$likes = Number::format($likes); |
84
|
|
|
|
85
|
|
|
if ($this->_writeCache($likes, 'ArticlesLikes')) { |
86
|
|
|
return $likes; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Re-count the number of user and find the latest user and write it in the Cache. |
94
|
|
|
* |
95
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
96
|
|
|
* |
97
|
|
|
* @return array|false |
98
|
|
|
*/ |
99
|
|
|
public function newUserStats(Event $event) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$this->Users = TableRegistry::get('Users'); |
|
|
|
|
102
|
|
|
|
103
|
|
|
$totalUsers = $this->Users->find()->count(); |
104
|
|
|
$totalUsers = Number::format($totalUsers); |
105
|
|
|
|
106
|
|
|
$lastRegistered = $this->Users->find('short')->order(['Users.created' => 'DESC'])->first(); |
107
|
|
|
|
108
|
|
|
$data = []; |
109
|
|
|
$data['TotalUsers'] = $totalUsers; |
110
|
|
|
$data['LastRegistered'] = $lastRegistered; |
111
|
|
|
|
112
|
|
|
if ($this->_writeCache($data, 'Users')) { |
|
|
|
|
113
|
|
|
return $data; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the Groups and write it in the Cache. |
121
|
|
|
* |
122
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
123
|
|
|
* |
124
|
|
|
* @return array|false |
125
|
|
|
*/ |
126
|
|
|
public function updateGroupStats(Event $event) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$this->Groups = TableRegistry::get('Groups'); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$groups = $this->Groups->find('translations')->order(['Groups.id' => 'DESC'])->toArray(); |
131
|
|
|
|
132
|
|
|
if ($this->_writeCache($groups, 'Groups')) { |
|
|
|
|
133
|
|
|
return $groups; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Write the data into the Cache with the passed key. |
141
|
|
|
* |
142
|
|
|
* @param int|object|string $data The data to save in the Cache. |
143
|
|
|
* @param string $key The key to save the data. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
protected function _writeCache($data, $key) |
148
|
|
|
{ |
149
|
|
|
if (empty($data) || empty($key)) { |
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$result = Cache::write($key, $data, 'statistics'); |
154
|
|
|
if ($result) { |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.