Completed
Push — master ( 3bf9b5...74e13e )
by Thomas Mauro
02:41
created

Sentry::sanitizeContextItem()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 8.8571
cc 5
eloc 7
nc 8
nop 1
crap 5
1
<?php
2
3
namespace Facile\SentryModule\Log\Writer;
4
5
use Facile\SentryModule\Service\Client;
6
use Zend\Log\Writer\AbstractWriter;
7
use Zend\Log\Logger;
8
use \Raven_Client;
9
10
/**
11
 * Class Sentry
12
 *
13
 * @package Facile\SentryModule\Log\Writer
14
 */
15
class Sentry extends AbstractWriter
16
{
17
    /**
18
     * @var Client
19
     */
20
    protected $client;
21
22
    /**
23
     * @var array
24
     */
25
    protected $priorityMap = [
26
        Logger::EMERG => Raven_Client::FATAL,
27
        Logger::ALERT => Raven_Client::ERROR,
28
        Logger::CRIT => Raven_Client::ERROR,
29
        Logger::ERR => Raven_Client::ERROR,
30
        Logger::WARN => Raven_Client::WARNING,
31
        Logger::NOTICE => Raven_Client::INFO,
32
        Logger::INFO => Raven_Client::INFO,
33
        Logger::DEBUG => Raven_Client::DEBUG,
34
    ];
35
36
    /**
37
     * Sentry constructor.
38
     *
39
     * @param array $options
40
     * @throws \RuntimeException
41
     * @throws \Zend\Log\Exception\InvalidArgumentException
42
     */
43 3
    public function __construct(array $options)
44
    {
45 3
        parent::__construct($options);
46
47 3
        if (!array_key_exists('client', $options)) {
48 1
            throw new \RuntimeException('No client specified in options');
49
        }
50
51 2
        $this->client = $options['client'];
52 2
    }
53
54
    /**
55
     * Write a message to the log
56
     *
57
     * @param array $event log data event
58
     * @return void
59
     */
60 1
    protected function doWrite(array $event)
61
    {
62 1
        $priority = $this->priorityMap[$event['priority']];
63
64 1
        $this->client->getRaven()->captureMessage(
65 1
            $event['message'],
66 1
            $this->sanitizeContextData($event['extra']),
67
            $priority
68 1
        );
69 1
    }
70
71
    /**
72
     * @param array $context
73
     *
74
     * @return array
75
     */
76 1
    protected function sanitizeContextData(array $context)
77
    {
78 1
        array_walk_recursive($context, [$this, 'sanitizeContextItem']);
79
80 1
        return $context;
81
    }
82
83
    /**
84
     * @param mixed $value
85
     */
86 1
    protected function sanitizeContextItem(&$value)
87
    {
88 1
        if ($value instanceof \Traversable) {
89 1
            $value = $this->sanitizeContextData(iterator_to_array($value));
90 1
        }
91 1
        if (is_object($value)) {
92 1
            $value = method_exists($value, '__toString') ? (string) $value : get_class($value);
93 1
        } elseif (is_resource($value)) {
94 1
            $value = get_resource_type($value);
95 1
        }
96 1
    }
97
}
98