|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\StatsBundle\EventListener; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\EventSubscriber; |
|
14
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
15
|
|
|
use LoginCidadao\OAuthBundle\Entity\ClientRepository; |
|
16
|
|
|
use LoginCidadao\StatsBundle\Entity\Statistics; |
|
17
|
|
|
use LoginCidadao\CoreBundle\Entity\Authorization; |
|
18
|
|
|
|
|
19
|
|
|
class StatisticsSubscriber implements EventSubscriber |
|
20
|
|
|
{ |
|
21
|
15 |
|
public function getSubscribedEvents() |
|
22
|
|
|
{ |
|
23
|
15 |
|
return ['postPersist', 'postRemove']; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param LifecycleEventArgs $args |
|
28
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function postPersist(LifecycleEventArgs $args) |
|
31
|
|
|
{ |
|
32
|
3 |
|
$this->authorizations($args); |
|
33
|
3 |
|
$this->authorizationsAggregate($args); |
|
34
|
3 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param LifecycleEventArgs $args |
|
38
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
39
|
|
|
*/ |
|
40
|
4 |
|
private function authorizations(LifecycleEventArgs $args) |
|
41
|
|
|
{ |
|
42
|
4 |
|
$entity = $args->getEntity(); |
|
43
|
4 |
|
if (!$entity instanceof Authorization) { |
|
44
|
2 |
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
$em = $args->getEntityManager(); |
|
48
|
2 |
|
$key = $entity->getClient()->getId(); |
|
49
|
|
|
|
|
50
|
|
|
/** @var ClientRepository $clientRepo */ |
|
51
|
2 |
|
$clientRepo = $em->getRepository('LoginCidadaoOAuthBundle:Client'); |
|
52
|
|
|
|
|
53
|
2 |
|
$count = $this->getClientCount($clientRepo, $entity); |
|
54
|
|
|
|
|
55
|
2 |
|
$statistics = new Statistics(); |
|
56
|
2 |
|
$statistics->setIndex('client.users') |
|
57
|
2 |
|
->setKey($key) |
|
58
|
2 |
|
->setTimestamp(new \DateTime()) |
|
59
|
2 |
|
->setValue($count); |
|
60
|
2 |
|
$em->persist($statistics); |
|
61
|
2 |
|
$em->flush($statistics); |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
private function getClientCount(ClientRepository $clientRepo, Authorization $entity) |
|
65
|
|
|
{ |
|
66
|
2 |
|
$counts = $clientRepo->getCountPerson( |
|
67
|
2 |
|
$entity->getPerson(), |
|
68
|
2 |
|
$entity->getClient()->getId() |
|
69
|
|
|
); |
|
70
|
2 |
|
if (count($counts) > 0) { |
|
71
|
1 |
|
$count = $counts[0]['qty']; |
|
72
|
1 |
|
$count = is_int($count) ? $count : 0; |
|
73
|
|
|
} else { |
|
74
|
1 |
|
$count = 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
return $count; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param LifecycleEventArgs $args |
|
82
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
83
|
|
|
*/ |
|
84
|
4 |
|
private function authorizationsAggregate(LifecycleEventArgs $args) |
|
85
|
|
|
{ |
|
86
|
4 |
|
$entity = $args->getEntity(); |
|
87
|
4 |
|
if (!($entity instanceof Authorization)) { |
|
88
|
2 |
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
$em = $args->getEntityManager(); |
|
92
|
2 |
|
$key = $entity->getClient()->getId(); |
|
93
|
2 |
|
$date = \DateTime::createFromFormat('Y-m-d H:i:s', date('Y-m-d 00:00:00')); |
|
94
|
|
|
|
|
95
|
|
|
/** @var ClientRepository $clientRepo */ |
|
96
|
2 |
|
$clientRepo = $em->getRepository('LoginCidadaoOAuthBundle:Client'); |
|
97
|
2 |
|
$statsRepo = $em->getRepository('LoginCidadaoStatsBundle:Statistics'); |
|
98
|
2 |
|
$count = $this->getClientCount($clientRepo, $entity); |
|
99
|
|
|
|
|
100
|
2 |
|
$statistics = $statsRepo->findOneBy([ |
|
101
|
2 |
|
'timestamp' => $date, |
|
102
|
2 |
|
'index' => 'agg.client.users', |
|
103
|
2 |
|
'key' => $key, |
|
104
|
|
|
]); |
|
105
|
2 |
|
if (!($statistics instanceof Statistics)) { |
|
106
|
2 |
|
$statistics = new Statistics(); |
|
107
|
2 |
|
$statistics->setIndex('agg.client.users') |
|
108
|
2 |
|
->setKey($key) |
|
109
|
2 |
|
->setTimestamp($date); |
|
|
|
|
|
|
110
|
2 |
|
$em->persist($statistics); |
|
111
|
|
|
} |
|
112
|
2 |
|
$statistics->setValue($count); |
|
113
|
2 |
|
$em->flush($statistics); |
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param LifecycleEventArgs $args |
|
118
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function postRemove(LifecycleEventArgs $args) |
|
121
|
|
|
{ |
|
122
|
1 |
|
$this->authorizations($args); |
|
123
|
1 |
|
$this->authorizationsAggregate($args); |
|
124
|
1 |
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|