Test Failed
Push — main ( d2d042...ea42f2 )
by Bingo
13:21
created

MetricsCollectionTask::run()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 2
nop 0
1
<?php
2
3
namespace Jabe\Engine\Impl\Metrics\Reporter;
4
5
use Jabe\Engine\Impl\ProcessEngineLogger;
6
use Jabe\Engine\Impl\Interceptor\CommandExecutorInterface;
7
use Jabe\Engine\Impl\Metrics\{
8
    Meter,
9
    MetricsLogger,
10
    MetricsRegistry
11
};
12
use Jabe\Engine\Impl\Persistence\Entity\MeterLogEntity;
13
use Jabe\Engine\Impl\Util\ClockUtil;
14
use Jabe\Engine\Impl\Util\Concurrent\TimerTask;
15
16
class MetricsCollectionTask extends TimerTask
17
{
18
    //private final static MetricsLogger LOG = ProcessEngineLogger.METRICS_LOGGER;
19
    protected $metricsRegistry;
20
    protected $commandExecutor;
21
    protected $reporterId = null;
22
23
    public function __construct(MetricsRegistry $metricsRegistry, CommandExecutorInterface $commandExecutor)
24
    {
25
        $this->metricsRegistry = $metricsRegistry;
26
        $this->commandExecutor = $commandExecutor;
27
    }
28
29
    public function run(): void
30
    {
31
        try {
32
            $this->collectMetrics();
33
        } catch (\Exception $e) {
34
            try {
35
                //LOG.couldNotCollectAndLogMetrics(e);
36
            } catch (\Exception $ex) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $ex) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
37
                // ignore if log can't be written
38
            }
39
        }
40
    }
41
42
    protected function collectMetrics(): void
43
    {
44
        $logs = [];
45
        foreach (array_values($this->metricsRegistry->getDbMeters()) as $meter) {
46
            $logs[] = new MeterLogEntity(
47
                $meter->getName(),
48
                $this->reporterId,
49
                $meter->getAndClear(),
50
                ClockUtil::getCurrentTime()->format('c')
51
            );
52
        }
53
        $this->commandExecutor->execute(new MetricsCollectionCmd($logs));
54
    }
55
56
    public function getReporter(): ?string
57
    {
58
        return $this->reporterId;
59
    }
60
61
    public function setReporter(string $reporterId): void
62
    {
63
        $this->reporterId = $reporterId;
64
    }
65
}
66