BatchHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 47
ccs 18
cts 18
cp 1
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 3 1
A handleBatch() 0 13 4
A getHighestRecord() 0 10 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Artack\Monolog\JiraHandler;
6
7
use Monolog\Handler\AbstractProcessingHandler;
8
9
/**
10
 * Base class for all batch handlers.
11
 *
12
 * @author ARTACK WebLab GmbH
13
 */
14
abstract class BatchHandler extends AbstractProcessingHandler
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 4
    public function handleBatch(array $records): void
20
    {
21 4
        $batchRecords = [];
22
23 4
        foreach ($records as $record) {
24 4
            if ($record['level'] < $this->level) {
25 3
                continue;
26
            }
27 3
            $batchRecords[] = $this->processRecord($record);
28
        }
29
30 4
        if (!empty($batchRecords)) {
31 3
            $this->send((string) $this->getFormatter()->formatBatch($batchRecords), $batchRecords);
32
        }
33 4
    }
34
35
    /**
36
     * Send a mail with the given content.
37
     *
38
     * @param string $content formatted email body to be sent
39
     * @param array  $records the array of log records that formed this content
40
     */
41
    abstract protected function send($content, array $records);
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    protected function write(array $record): void
47
    {
48 1
        $this->send((string) $record['formatted'], [$record]);
49 1
    }
50
51 2
    protected function getHighestRecord(array $records)
52
    {
53 2
        $highestRecord = null;
54 2
        foreach ($records as $record) {
55 2
            if (null === $highestRecord || $highestRecord['level'] < $record['level']) {
56 2
                $highestRecord = $record;
57
            }
58
        }
59
60 2
        return $highestRecord;
61
    }
62
}
63