Passed
Push — master ( 184cb3...5f1713 )
by Waaaaaaaaaa
02:19
created

Logfile::sendAsync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Logfile;
4
5
use Throwable;
6
7
class Logfile
8
{
9
    protected $token;
10
11
    protected $sender;
12
13
    protected $config;
14
15
    protected $async = false;
16
17
    public function __construct(string $token, Config $config = null)
18
    {
19
        $this->token = $token;
20
        $this->sender = new Sender();
21
        $this->config = $config ?: new Config();
22
    }
23
24
    public function sendAsync(bool $async): void
25
    {
26
        $this->async = $async;
27
    }
28
29
    public function getSender(): Sender
30
    {
31
        return $this->sender;
32
    }
33
34
    public function setConfig(Config $config): void
35
    {
36
        $this->config = $config;
37
    }
38
39
    public function getConfig(): Config
40
    {
41
        return $this->config;
42
    }
43
44
    protected function getToken(): string
45
    {
46
        return $this->token;
47
    }
48
49
    /**
50
     * Capture exception and return the event ID
51
     *
52
     * @param Throwable $exception
53
     * @return string
54
     */
55
    public function captureException(Throwable $exception): string
56
    {
57
        $payload = Payload::createFromException($exception, $this->getConfig());
58
59
        return $this->log($payload);
60
    }
61
62
    /**
63
     * Log payload
64
     *
65
     * @param Payload $payload
66
     * @return string
67
     */
68
    public function log(Payload $payload): string
69
    {
70
        $this->sender->{$this->async ? 'sendAsync' : 'send'}($payload, $this->getToken());
71
        return $payload->getId();
72
    }
73
}
74