Completed
Push — master ( 5fa5de...689521 )
by Matze
03:31
created

Controller::getJobs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4286
cc 2
eloc 5
nc 2
nop 2
crap 6
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 1
        $stats = array_merge($stats, [
49 1
            'message_queue:queued' => $this->messageQueue->countAllJobs(),
50
        ]);
51
52
        try {
53 1
            $redisStats = $this->getRedis()->info();
54
        } catch (PredisException $e) {
55
            $redisStats = [];
56
        }
57
58
        return [
59 1
            'jobs'  => $this->messageQueue->getEventsByType(),
60 1
            'stats' => $stats,
61 1
            'redis' => $redisStats
62
        ];
63
    }
64
65
    /**
66
     * @Route("/stats/reset/", methods="POST", name="stats.reset")
67
     * @param Request $request
68
     * @return bool
69
     */
70 1
    public function resetStats(Request $request)
71
    {
72 1
        $key = $request->request->get('key');
73 1
        $this->stats->set([$key => 0]);
74
75 1
        return true;
76
    }
77
}
78