Completed
Push — master ( c4b22f...ba9072 )
by Matze
08:46
created

Controller::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 16
ccs 6
cts 8
cp 0.75
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace BrainExe\Core\Stats;
4
5
use BrainExe\Annotations\Annotations\Inject;
6
use BrainExe\Core\Annotations\Controller as ControllerAnnotation;
7
use BrainExe\Core\Annotations\Route;
8
use BrainExe\Core\MessageQueue\Gateway as MessageQueueGateway;
9
use BrainExe\Core\Traits\RedisTrait;
10
use Predis\PredisException;
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * @ControllerAnnotation("Stats.Controller")
15
 */
16
class Controller
17
{
18
    use RedisTrait;
19
20
    /**
21
     * @var MessageQueueGateway
22
     */
23
    private $messageQueue;
24
25
    /**
26
     * @var Stats
27
     */
28
    private $stats;
29
30
    /**
31
     * @Inject({"@Stats.Stats", "@MessageQueue.Gateway"})
32
     * @param Stats $stats
33
     * @param MessageQueueGateway $gateway
34
     */
35 2
    public function __construct(Stats $stats, MessageQueueGateway $gateway)
36
    {
37 2
        $this->messageQueue = $gateway;
38 2
        $this->stats        = $stats;
39 2
    }
40
41
    /**
42
     * @Route("/stats/", name="status.index")
43
     */
44 1
    public function index()
45
    {
46 1
        $stats = $this->stats->getAll();
47
48
        try {
49 1
            $redisStats = $this->getRedis()->info();
50
        } catch (PredisException $e) {
51
            $redisStats = [];
52
        }
53
54
        return [
55 1
            'jobs'  => $this->messageQueue->getEventsByType(),
56 1
            'stats' => $stats,
57 1
            'redis' => $redisStats
58
        ];
59
    }
60
61
    /**
62
     * @Route("/stats/reset/", methods="POST", name="stats.reset")
63
     * @param Request $request
64
     * @return bool
65
     */
66 1
    public function resetStats(Request $request)
67
    {
68 1
        $key = $request->request->get('key');
69 1
        $this->stats->set([$key => 0]);
70
71 1
        return true;
72
    }
73
}
74