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

Controller::deleteJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
crap 1
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