Completed
Push — master ( cd52e5...2b62de )
by Artem
01:26
created

CentrifugoCollector::collect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
/*
3
 * This file is part of the FreshCentrifugoBundle.
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\CentrifugoBundle\DataCollector;
14
15
use Fresh\CentrifugoBundle\Logger\CommandHistoryLogger;
16
use Fresh\CentrifugoBundle\Model\CommandInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
20
21
/**
22
 * CentrifugoCollector.
23
 *
24
 * @author Artem Henvald <[email protected]>
25
 */
26
class CentrifugoCollector extends DataCollector
27
{
28
    /** @var CommandHistoryLogger */
29
    private $commandHistoryLogger;
30
31
    /**
32
     * @param CommandHistoryLogger $commandHistoryLogger
33
     */
34
    public function __construct(CommandHistoryLogger $commandHistoryLogger)
35
    {
36
        $this->commandHistoryLogger = $commandHistoryLogger;
37
        $this->reset();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function collect(Request $request, Response $response, \Throwable $exception = null): void
44
    {
45
        $this->data = [
46
            'command_history' => $this->commandHistoryLogger->getCommandHistory(),
47
            'requests_count' => $this->commandHistoryLogger->getRequestsCount(),
48
            'commands_count' => $this->commandHistoryLogger->getCommandsCount(),
49
            'successful_commands_count' => $this->commandHistoryLogger->getSuccessfulCommandsCount(),
50
            'failed_commands_count' => $this->commandHistoryLogger->getFailedCommandsCount(),
51
        ];
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getName(): string
58
    {
59
        return 'centrifugo';
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function reset(): void
66
    {
67
        $this->data = [
68
            'command_history' => [],
69
            'requests_count' => 0,
70
            'commands_count' => 0,
71
            'successful_commands_count' => 0,
72
            'failed_commands_count' => 0,
73
        ];
74
        $this->commandHistoryLogger->clearCommandHistory();
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function getCommandsCount(): int
81
    {
82
        return $this->data['commands_count'];
83
    }
84
85
    /**
86
     * @return int
87
     */
88
    public function getSuccessfulCommandsCount(): int
89
    {
90
        return $this->data['successful_commands_count'];
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    public function getFailedCommandsCount(): int
97
    {
98
        return $this->data['failed_commands_count'];
99
    }
100
101
    /**
102
     * @return int
103
     */
104
    public function getRequestsCount(): int
105
    {
106
        return $this->data['requests_count'];
107
    }
108
109
    /**
110
     * @return CommandInterface[]
111
     */
112
    public function getCommandHistory(): array
113
    {
114
        return $this->data['command_history'];
115
    }
116
}
117