Completed
Push — master ( 83b55d...472d84 )
by Justin
03:47 queued 36s
created

RollbarLogger::getDebugLogFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace Rollbar;
2
3
use Psr\Log\AbstractLogger;
0 ignored issues
show
Bug introduced by
The type Psr\Log\AbstractLogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use Rollbar\Payload\Payload;
5
use Rollbar\Payload\Level;
6
use Rollbar\Truncation\Truncation;
7
use Monolog\Logger as MonologLogger;
0 ignored issues
show
Bug introduced by
The type Monolog\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Monolog\Handler\StreamHandler;
0 ignored issues
show
Bug introduced by
The type Monolog\Handler\StreamHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Rollbar\Payload\EncodedPayload;
10
11
class RollbarLogger extends AbstractLogger
12
{
13
    private $config;
14
    private $levelFactory;
15
    private $truncation;
16
    private $queue;
17
    private $debugLogger;
18
    private $debugLogFile;
19
20
    public function __construct(array $config)
21
    {
22
        $this->config = new Config($config);
23
        $this->levelFactory = new LevelFactory();
24
        $this->truncation = new Truncation($this->config);
25
        $this->queue = array();
26
        
27
        $this->debugLogFile = sys_get_temp_dir() . '/rollbar.debug.log';
28
        $this->debugLogger = new MonologLogger("RollbarDebugLogger");
29
        $this->debugLogger->pushHandler(new StreamHandler(
30
            $this->debugLogFile,
31
            $this->config->getVerbosity()
32
        ));
33
    }
34
    
35
    public function enable()
36
    {
37
        return $this->config->enable();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->config->enable() targeting Rollbar\Config::enable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38
    }
39
    
40
    public function disable()
41
    {
42
        return $this->config->disable();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->config->disable() targeting Rollbar\Config::disable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
    }
44
    
45
    public function enabled()
46
    {
47
        return $this->config->enabled();
48
    }
49
    
50
    public function disabled()
51
    {
52
        return $this->config->disabled();
53
    }
54
55
    public function configure(array $config)
56
    {
57
        $this->config->configure($config);
58
    }
59
60
    public function scope(array $config)
61
    {
62
        return new RollbarLogger($this->extend($config));
63
    }
64
65
    public function extend(array $config)
66
    {
67
        return $this->config->extend($config);
68
    }
69
    
70
    public function addCustom($key, $data)
71
    {
72
        $this->config->addCustom($key, $data);
73
    }
74
    
75
    public function removeCustom($key)
76
    {
77
        $this->config->removeCustom($key);
78
    }
79
    
80
    public function getCustom()
81
    {
82
        return $this->config->getCustom();
83
    }
84
85
    public function log($level, $toLog, array $context = array(), $isUncaught = false)
86
    {
87
        if ($this->disabled()) {
88
            return new Response(0, "Disabled");
89
        }
90
        
91
        if (!$this->levelFactory->isValidLevel($level)) {
92
            throw new \Psr\Log\InvalidArgumentException("Invalid log level '$level'.");
0 ignored issues
show
Bug introduced by
The type Psr\Log\InvalidArgumentException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
        }
94
        if ($this->config->internalCheckIgnored($level, $toLog)) {
95
            return new Response(0, "Ignored");
96
        }
97
        $accessToken = $this->getAccessToken();
98
        $payload = $this->getPayload($accessToken, $level, $toLog, $context);
99
        
100
        if ($this->config->checkIgnored($payload, $accessToken, $toLog, $isUncaught)) {
101
            $response = new Response(0, "Ignored");
102
        } else {
103
            $serialized = $payload->serialize();
104
            $scrubbed = $this->scrub($serialized);
105
            $encoded = $this->encode($scrubbed);
106
            $truncated = $this->truncate($encoded);
107
            
108
            $this->debugLogger->info(
109
                "Payload scrubbed and ready to send to ".
110
                $this->config->getSender()->toString()
111
            );
112
            $this->debugLogger->debug($truncated);
113
            
114
            $response = $this->send($truncated, $accessToken);
115
            
116
            $this->debugLogger->info("Received response from Rollbar API.");
117
            $this->debugLogger->debug(print_r($response, true));
118
        }
119
        
120
        $this->handleResponse($payload, $response);
121
        
122
        return $response;
123
    }
124
125
    public function flush()
126
    {
127
        if ($this->getQueueSize() > 0) {
128
            $batch = $this->queue;
129
            $this->queue = array();
130
            return $this->config->sendBatch($batch, $this->getAccessToken());
131
        }
132
        return new Response(0, "Queue empty");
133
    }
134
135
    public function flushAndWait()
136
    {
137
        $this->flush();
138
        $this->config->wait($this->getAccessToken());
139
    }
140
141
    public function shouldIgnoreError($errno)
142
    {
143
        return $this->config->shouldIgnoreError($errno);
144
    }
145
146
    public function getQueueSize()
147
    {
148
        return count($this->queue);
149
    }
150
151
    protected function send(\Rollbar\Payload\EncodedPayload $payload, $accessToken)
152
    {
153
        if ($this->config->getBatched()) {
154
            $response = new Response(0, "Pending");
155
            if ($this->getQueueSize() >= $this->config->getBatchSize()) {
156
                $response = $this->flush();
157
            }
158
            $this->queue[] = $payload;
159
            return $response;
160
        }
161
        return $this->config->send($payload, $accessToken);
162
    }
163
164
    protected function getPayload($accessToken, $level, $toLog, $context)
165
    {
166
        $data = $this->config->getRollbarData($level, $toLog, $context);
167
        $payload = new Payload($data, $accessToken);
168
        return $this->config->transform($payload, $level, $toLog, $context);
169
    }
170
171
    protected function getAccessToken()
172
    {
173
        return $this->config->getAccessToken();
174
    }
175
    
176
    public function getDataBuilder()
177
    {
178
        return $this->config->getDataBuilder();
179
    }
180
    
181
    public function getDebugLogFile()
182
    {
183
        return $this->debugLogFile;
184
    }
185
186
    protected function handleResponse($payload, $response)
187
    {
188
        $this->config->handleResponse($payload, $response);
189
    }
190
    
191
    /**
192
     * @param array $serializedPayload
193
     * @return array
194
     */
195
    protected function scrub(array &$serializedPayload)
196
    {
197
        $serializedPayload['data'] = $this->config->getScrubber()->scrub($serializedPayload['data']);
198
        return $serializedPayload;
199
    }
200
    
201
    /**
202
     * @param \Rollbar\Payload\EncodedPayload $payload
203
     * @return \Rollbar\Payload\EncodedPayload
204
     */
205
    protected function truncate(\Rollbar\Payload\EncodedPayload &$payload)
206
    {
207
        return $this->truncation->truncate($payload);
208
    }
209
    
210
    /**
211
     * @param array &$payload
212
     * @return \Rollbar\Payload\EncodedPayload
213
     */
214
    protected function encode(array &$payload)
215
    {
216
        $encoded = new EncodedPayload($payload);
217
        $encoded->encode();
218
        return $encoded;
219
    }
220
}
221