CompressedJsonEncoder   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 28
rs 10
ccs 4
cts 4
cp 1
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encode() 0 5 1
A __construct() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the php-gelf package.
6
 *
7
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Gelf\Encoder;
14
15
use Gelf\MessageInterface;
16
17
/**
18
 * The CompressedJsonEncoder allows the encoding of GELF messages as described
19
 * in http://www.graylog2.org/resources/documentation/sending/gelfhttp
20
 *
21
 * @author Benjamin Zikarsky <[email protected]>
22
 */
23
class CompressedJsonEncoder implements EncoderInterface
24
{
25
    const DEFAULT_COMPRESSION_LEVEL = -1;
26
27
    private JsonEncoder $jsonEncoder;
28
29
    /**
30
     * Class constructor
31
     *
32
     * Allows the specification of the gzip compression-level
33
     */
34
    public function __construct(
35
        private int $compressionLevel = self::DEFAULT_COMPRESSION_LEVEL
36
    ) {
37
        $this->jsonEncoder = new JsonEncoder();
38
    }
39
40
    /**
41 7
     * Encodes a given message
42
     *
43
     * @param  MessageInterface $message
44 7
     * @return string
45 7
     */
46 7
    public function encode(MessageInterface $message): string
47
    {
48
        $json = $this->jsonEncoder->encode($message);
49
50
        return gzcompress($json, $this->compressionLevel);
51
    }
52
}
53