Passed
Push — master ( ddd97a...25aa58 )
by Harry
06:46
created

RaygunHandler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDefaultFormatter() 0 3 1
A writeException() 0 3 1
B isHandling() 0 17 8
C write() 0 25 7
A writeError() 0 11 1
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
namespace Graze\Monolog\Handler;
14
15
use Graze\Monolog\Formatter\RaygunFormatter;
16
use Monolog\Handler\AbstractProcessingHandler;
17
use Monolog\Logger;
18
use Raygun4php\RaygunClient;
19
20
class RaygunHandler extends AbstractProcessingHandler
21
{
22
    /**
23
     * @var RaygunClient
24
     */
25
    protected $client;
26
27
    /**
28
     * @param RaygunClient $client
29
     * @param int $level
30
     * @param bool $bubble
31
     */
32
    public function __construct(RaygunClient $client, $level = Logger::DEBUG, $bubble = true)
33
    {
34
        $this->client = $client;
35
36
        parent::__construct($level, $bubble);
37
    }
38
39
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$record" missing
Loading history...
40
     * {@inheritdoc}
41
     */
42
    public function isHandling(array $record)
43
    {
44
        if(parent::isHandling($record)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
45
            $context = $record['context'];
46
47
            //Ensure only valid records will be handled and no InvalidArgumentException will be thrown
48
            if ((isset($context['exception']) &&
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (IssetNode && $context['...ode && $context['line'], Probably Intended Meaning: IssetNode && ($context['...de && $context['line'])
Loading history...
49
                    (
50
                        $context['exception'] instanceof \Exception ||
51
                        (PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable)
52
                    )
53
                ) || (isset($context['file']) && $context['line'])
54
            ) {
55
                return true;
56
            }
57
        }
58
        return false;
59
    }
60
61
    /**
62
     * @param array $record
63
     */
64
    protected function write(array $record)
65
    {
66
        $context = $record['context'];
67
68
        if (isset($context['exception']) &&
69
            (
70
                $context['exception'] instanceof \Exception ||
71
                (PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable)
72
            )
73
        ) {
74
            $this->writeException(
75
                $record,
76
                $record['formatted']['tags'],
77
                $record['formatted']['custom_data'],
78
                $record['formatted']['timestamp']
79
            );
80
        } elseif (isset($context['file']) && $context['line']) {
81
            $this->writeError(
82
                $record['formatted'],
83
                $record['formatted']['tags'],
84
                $record['formatted']['custom_data'],
85
                $record['formatted']['timestamp']
86
            );
87
        } else {
88
            throw new \InvalidArgumentException('Invalid record given.');
89
        }
90
    }
91
92
    /**
93
     * @param array $record
94
     * @param array $tags
95
     * @param array $customData
96
     * @param int|float $timestamp
97
     */
98
    protected function writeError(array $record, array $tags = array(), array $customData = array(), $timestamp = null)
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
99
    {
100
        $context = $record['context'];
101
        $this->client->SendError(
102
            0,
103
            $record['message'],
104
            $context['file'],
105
            $context['line'],
106
            $tags,
107
            $customData,
108
            $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

108
            /** @scrutinizer ignore-type */ $timestamp
Loading history...
109
        );
110
    }
111
112
    /**
113
     * @param array $record
114
     * @param array $tags
115
     * @param array $customData
116
     * @param int|float $timestamp
117
     */
118
    protected function writeException(array $record, array $tags = array(), array $customData = array(), $timestamp = null)
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
119
    {
120
        $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

120
        $this->client->SendException($record['context']['exception'], $tags, $customData, /** @scrutinizer ignore-type */ $timestamp);
Loading history...
121
    }
122
123
    /**
124
     * @return \Monolog\Formatter\FormatterInterface
125
     */
126
    protected function getDefaultFormatter()
127
    {
128
        return new RaygunFormatter();
129
    }
130
}
131
132