Completed
Push — master ( 8c4272...ef6d68 )
by Thomas Mauro
11s
created

Sentry   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 64
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 14 5
A doWrite() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\SentryModule\Log\Writer;
6
7
use Facile\Sentry\Common\Sender\SenderInterface;
8
use Traversable;
9
use Zend\Log\Writer\AbstractWriter;
10
use Zend\Log\Logger;
11
use Raven_Client;
12
use Facile\SentryModule\Exception;
13
14
/**
15
 * Class Sentry.
16
 */
17
final class Sentry extends AbstractWriter
18
{
19
    /**
20
     * @var SenderInterface
21
     */
22
    private $sender;
23
24
    /**
25
     * @var array
26
     */
27
    protected $priorityMap = [
28
        Logger::EMERG => Raven_Client::FATAL,
29
        Logger::ALERT => Raven_Client::ERROR,
30
        Logger::CRIT => Raven_Client::ERROR,
31
        Logger::ERR => Raven_Client::ERROR,
32
        Logger::WARN => Raven_Client::WARNING,
33
        Logger::NOTICE => Raven_Client::INFO,
34
        Logger::INFO => Raven_Client::INFO,
35
        Logger::DEBUG => Raven_Client::DEBUG,
36
    ];
37
38
    /**
39
     * Sentry constructor.
40
     *
41
     * @param array|Traversable $options
42
     *
43
     * @throws \Zend\Log\Exception\InvalidArgumentException
44
     * @throws Exception\InvalidArgumentException
45
     */
46 6
    public function __construct($options = null)
47
    {
48 6
        parent::__construct($options);
49
50 6
        if ($options instanceof Traversable) {
51 2
            $options = iterator_to_array($options);
52
        }
53
54 6
        if (! is_array($options) || ! array_key_exists('sender', $options) || ! $options['sender'] instanceof SenderInterface) {
55 1
            throw new Exception\InvalidArgumentException('No sender specified in options');
56
        }
57
58 5
        $this->sender = $options['sender'];
59 5
    }
60
61
    /**
62
     * Write a message to the log.
63
     *
64
     * @param array $event log data event
65
     */
66 3
    protected function doWrite(array $event)
67
    {
68 3
        $priority = $this->priorityMap[$event['priority']];
69 3
        $message = $event['message'];
70 3
        $context = $event['extra'] ?? [];
71
72 3
        if ($context instanceof Traversable) {
73 1
            $context = iterator_to_array($context);
74 2
        } elseif (! is_array($context)) {
75 1
            $context = [];
76
        }
77
78 3
        $this->sender->send($priority, (string) $message, $context);
79 3
    }
80
}
81