NatsDataCollector   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 82
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A collect() 0 6 2
A getCommands() 0 4 1
A getCommandCount() 0 4 1
A getErrorCommandsCount() 0 6 1
A getTime() 0 9 2
A getName() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the NatsBundle package.
5
 *
6
 * (c) Issel Guberna <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Octante\NatsBundle\DataCollector;
13
14
use Octante\NatsBundle\Logger\NatsLogger;
15
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * NatsDataCollector
21
 */
22
class NatsDataCollector extends DataCollector
23
{
24
    /**
25
     * @var NatsLogger
26
     */
27
    protected $logger;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param NatsLogger $logger
33
     */
34
    public function __construct(NatsLogger $logger)
35
    {
36
        $this->logger = $logger;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function collect(Request $request, Response $response, \Exception $exception = null)
43
    {
44
        $this->data = array(
45
            'commands' => null !== $this->logger ? $this->logger->getCommands() : array(),
46
        );
47
    }
48
49
    /**
50
     * Returns an array of collected commands.
51
     *
52
     * @return array
53
     */
54
    public function getCommands()
55
    {
56
        return $this->data['commands'];
57
    }
58
59
    /**
60
     * Returns the number of collected commands.
61
     *
62
     * @return integer
63
     */
64
    public function getCommandCount()
65
    {
66
        return count($this->data['commands']);
67
    }
68
69
    /**
70
     * Returns the number of failed commands.
71
     *
72
     * @return integer
73
     */
74
    public function getErrorCommandsCount()
75
    {
76
        return count(array_filter($this->data['commands'], function ($command) {
77
            return $command['error'] !== false;
78
        }));
79
    }
80
81
    /**
82
     * Returns the execution time of all collected commands in seconds.
83
     *
84
     * @return float
85
     */
86
    public function getTime()
87
    {
88
        $time = 0;
89
        foreach ($this->data['commands'] as $command) {
90
            $time += $command['executionMS'];
91
        }
92
93
        return $time;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getName()
100
    {
101
        return 'nats';
102
    }
103
}