Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Spatie\FlareClient;
4
 
5
use Exception;
6
use Spatie\FlareClient\Http\Client;
7
use Spatie\FlareClient\Truncation\ReportTrimmer;
8
 
9
class Api
10
{
11
    protected Client $client;
12
 
13
    protected bool $sendReportsImmediately = false;
14
 
15
    /** @var array<int, Report> */
16
    protected array $queue = [];
17
 
18
    public function __construct(Client $client)
19
    {
20
        $this->client = $client;
21
 
22
        register_shutdown_function([$this, 'sendQueuedReports']);
23
    }
24
 
25
    public function sendReportsImmediately(): self
26
    {
27
        $this->sendReportsImmediately = true;
28
 
29
        return $this;
30
    }
31
 
32
    public function report(Report $report): void
33
    {
34
        try {
35
            $this->sendReportsImmediately
36
                ? $this->sendReportToApi($report)
37
                : $this->addReportToQueue($report);
38
        } catch (Exception $e) {
39
            //
40
        }
41
    }
42
 
43
    public function sendTestReport(Report $report): self
44
    {
45
        $this->sendReportToApi($report);
46
 
47
        return $this;
48
    }
49
 
50
    protected function addReportToQueue(Report $report): self
51
    {
52
        $this->queue[] = $report;
53
 
54
        return $this;
55
    }
56
 
57
    public function sendQueuedReports(): void
58
    {
59
        try {
60
            foreach ($this->queue as $report) {
61
                $this->sendReportToApi($report);
62
            }
63
        } catch (Exception $e) {
64
            //
65
        } finally {
66
            $this->queue = [];
67
        }
68
    }
69
 
70
    protected function sendReportToApi(Report $report): void
71
    {
72
        $payload = $this->truncateReport($report->toArray());
73
 
74
        $this->client->post('reports', $payload);
75
    }
76
 
77
    /**
78
     * @param array<int|string, mixed> $payload
79
     *
80
     * @return array<int|string, mixed>
81
     */
82
    protected function truncateReport(array $payload): array
83
    {
84
        return (new ReportTrimmer())->trim($payload);
85
    }
86
}