Completed
Pull Request — master (#14)
by Artem
06:19
created

CentrifugoCollector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 91
rs 10
c 0
b 0
f 0
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
    private CommandHistoryLogger $commandHistoryLogger;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
29
30
    /**
31
     * @param CommandHistoryLogger $commandHistoryLogger
32
     */
33
    public function __construct(CommandHistoryLogger $commandHistoryLogger)
34
    {
35
        $this->commandHistoryLogger = $commandHistoryLogger;
36
        $this->reset();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function collect(Request $request, Response $response, \Throwable $exception = null): void
43
    {
44
        $this->data = [
45
            'command_history' => $this->commandHistoryLogger->getCommandHistory(),
46
            'requests_count' => $this->commandHistoryLogger->getRequestsCount(),
47
            'commands_count' => $this->commandHistoryLogger->getCommandsCount(),
48
            'successful_commands_count' => $this->commandHistoryLogger->getSuccessfulCommandsCount(),
49
            'failed_commands_count' => $this->commandHistoryLogger->getFailedCommandsCount(),
50
        ];
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getName(): string
57
    {
58
        return 'centrifugo';
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function reset(): void
65
    {
66
        $this->data = [
67
            'command_history' => [],
68
            'requests_count' => 0,
69
            'commands_count' => 0,
70
            'successful_commands_count' => 0,
71
            'failed_commands_count' => 0,
72
        ];
73
        $this->commandHistoryLogger->clearCommandHistory();
74
    }
75
76
    /**
77
     * @return int
78
     */
79
    public function getCommandsCount(): int
80
    {
81
        return $this->data['commands_count'];
82
    }
83
84
    /**
85
     * @return int
86
     */
87
    public function getSuccessfulCommandsCount(): int
88
    {
89
        return $this->data['successful_commands_count'];
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function getFailedCommandsCount(): int
96
    {
97
        return $this->data['failed_commands_count'];
98
    }
99
100
    /**
101
     * @return int
102
     */
103
    public function getRequestsCount(): int
104
    {
105
        return $this->data['requests_count'];
106
    }
107
108
    /**
109
     * @return CommandInterface[]
110
     */
111
    public function getCommandHistory(): array
112
    {
113
        return $this->data['command_history'];
114
    }
115
}
116