Completed
Push — master ( 6eed73...c375a7 )
by
unknown
57:50
created

Logstash   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A write() 0 11 3
A handleError() 0 4 1
1
<?php
2
3
namespace Clearcode\SimpleBusElkBundle\Logstash;
4
5
use Clearcode\SimpleBusElkBundle\Converter\DataToConvertIsNotAnObject;
6
use Clearcode\SimpleBusElkBundle\Converter\ObjectToArrayConverterInterface;
7
use Psr\Log\LoggerInterface;
8
9
class Logstash
10
{
11
    /** @var LoggerInterface */
12
    private $logger;
13
    /** @var ObjectToArrayConverterInterface */
14
    private $converter;
15
16
    /**
17
     * @param LoggerInterface                 $logger
18
     * @param ObjectToArrayConverterInterface $converter
19
     */
20
    public function __construct(LoggerInterface $logger, ObjectToArrayConverterInterface $converter)
21
    {
22
        $this->logger = $logger;
23
        $this->converter = $converter;
24
    }
25
26
    /**
27
     * @param $object
28
     *
29
     * @throws CannotWriteToLogstash
30
     */
31
    public function write($object)
32
    {
33
        try {
34
            $this->logger->info('Event recorded', $this->converter->toArray($object));
35
36
        } catch (DataToConvertIsNotAnObject $e) {
37
            $this->handleError(sprintf('Data conversion problem during writing to logstash: %s', $e->getMessage()));
38
        } catch (\Exception $e) {
39
            $this->handleError(sprintf('Connection with logstash error: %s', $e->getMessage()));
40
        }
41
    }
42
43
    private function handleError($message)
44
    {
45
        throw new CannotWriteToLogstash($message);
46
    }
47
}
48