forked from PUGX/badge-poser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatsListener.php
68 lines (59 loc) · 1.83 KB
/
StatsListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/*
* This file is part of the badge-poser package
*
* (c) Giulio De Donato <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PUGX\StatsBundle\Listener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use PUGX\StatsBundle\Service\PersisterInterface;
/**
* Class StatsListener
* This class is intended to collect and store usage statistic on Redis.
*
* @package PUGX\BadgeBundle\Listener
*/
class StatsListener
{
private $client;
public function __construct(PersisterInterface $client)
{
$this->client = $client;
}
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
$request = $event->getRequest();
/*
* $controller passed can be either a class or a Closure. This is not usual in Symfony2 but it may happen.
* If it is a class, it comes in array format
*/
if (!is_array($controller) || null === $request) {
return;
}
$this->persistData($request, $controller[1]);
}
/**
* Persist data.
*
* @param Request $request The request
* @param string $controller The controller Name
*
* @return Boolean
*/
public function persistData(Request $request, $controller)
{
if (null === ($repository = $request->get('repository', null))) {
return;
}
$this->client->incrementTotalAccess();
$this->client->incrementRepositoryAccess($repository);
$this->client->addRepositoryToLatestAccessed($repository);
$this->client->incrementRepositoryAccessType($repository, $controller);
return true;
}
}