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

Controller   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 88.89%

Importance

Changes 9
Bugs 0 Features 3
Metric Value
wmc 4
c 9
b 0
f 3
lcom 1
cbo 3
dl 0
loc 62
ccs 16
cts 18
cp 0.8889
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 20 2
A resetStats() 0 7 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