BatchHandler::getHighestRecord()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 4
rs 10
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