Completed
Push — master ( 1d6c1f...26ec6b )
by Harry
06:29
created

RaygunHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultFormatter() 0 3 1
A writeException() 0 3 1
A writeError() 0 11 1
A __construct() 0 5 1
C write() 0 22 7
1
<?php
2
/*
3
 * This file is part of Monolog Extensions
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/MonologExtensions/blob/master/LICENSE
11
 * @link http://github.com/graze/MonologExtensions
12
 */
13
14
namespace Graze\Monolog\Handler;
15
16
use Graze\Monolog\Formatter\RaygunFormatter;
17
use Monolog\Handler\AbstractProcessingHandler;
18
use Monolog\Logger;
19
use Raygun4php\RaygunClient;
20
21
class RaygunHandler extends AbstractProcessingHandler
22
{
23
    /**
24
     * @var RaygunClient
25
     */
26
    protected $client;
27
28
    /**
29
     * @param RaygunClient $client
30
     * @param int          $level
31
     * @param bool         $bubble
32
     */
33
    public function __construct(RaygunClient $client, $level = Logger::DEBUG, $bubble = true)
34
    {
35
        $this->client = $client;
36
37
        parent::__construct($level, $bubble);
38
    }
39
40
    /**
41
     * @param array $record
42
     */
43
    protected function write(array $record)
44
    {
45
        $context = $record['context'];
46
47
        if (isset($context['exception'])
48
            && (
49
                $context['exception'] instanceof \Exception
50
                || (PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable)
51
            )
52
        ) {
53
            $this->writeException(
54
                $record,
55
                $record['formatted']['tags'],
56
                $record['formatted']['custom_data'],
57
                $record['formatted']['timestamp']
58
            );
59
        } elseif (isset($context['file']) && isset($context['line'])) {
60
            $this->writeError(
61
                $record['formatted'],
62
                $record['formatted']['tags'],
63
                $record['formatted']['custom_data'],
64
                $record['formatted']['timestamp']
65
            );
66
        }
67
        // do nothing if its not an exception or an error
68
    }
69
70
    /**
71
     * @param array     $record
72
     * @param array     $tags
73
     * @param array     $customData
74
     * @param int|float $timestamp
75
     */
76
    protected function writeError(array $record, array $tags = [], array $customData = [], $timestamp = null)
77
    {
78
        $context = $record['context'];
79
        $this->client->SendError(
80
            0,
81
            $record['message'],
82
            $context['file'],
83
            $context['line'],
84
            $tags,
85
            $customData,
86
            $timestamp
0 ignored issues
show
Bug introduced by
It seems like $timestamp can also be of type double; however, parameter $timestamp of Raygun4php\RaygunClient::SendError() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            /** @scrutinizer ignore-type */ $timestamp
Loading history...
87
        );
88
    }
89
90
    /**
91
     * @param array     $record
92
     * @param array     $tags
93
     * @param array     $customData
94
     * @param int|float $timestamp
95
     */
96
    protected function writeException(array $record, array $tags = [], array $customData = [], $timestamp = null)
97
    {
98
        $this->client->SendException($record['context']['exception'], $tags, $customData, $timestamp);
0 ignored issues
show
Bug introduced by
It seems like $timestamp can also be of type double; however, parameter $timestamp of Raygun4php\RaygunClient::SendException() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        $this->client->SendException($record['context']['exception'], $tags, $customData, /** @scrutinizer ignore-type */ $timestamp);
Loading history...
99
    }
100
101
    /**
102
     * @return \Monolog\Formatter\FormatterInterface
103
     */
104
    protected function getDefaultFormatter()
105
    {
106
        return new RaygunFormatter();
107
    }
108
}
109
110