OutgoingWebhookJob::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Bencoderus\Webhook\Jobs;
4
5
use Bencoderus\Webhook\Traits\SendWebhook;
6
use Illuminate\Bus\Queueable;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Foundation\Bus\Dispatchable;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Illuminate\Queue\SerializesModels;
11
12
class OutgoingWebhookJob implements ShouldQueue
13
{
14
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, SendWebhook;
15
16
    /**
17
     * The webhook data.
18
     *
19
     * @var array
20
     */
21
    public $webhookData;
22
23
    /**
24
     * The Number of times a webhook would be retried.
25
     *
26
     * @var int
27
     */
28
    public $tries = 5;
29
30
    /**
31
     * The number of minutes to wait before the webhook will be retried
32
     *
33
     * @var int
34
     */
35
    public $retryInterval;
36
37
    /**
38
     * Create a new job instance.
39
     *
40
     * @param array $webhookData
41
     */
42
    public function __construct(array $webhookData)
43
    {
44
        $this->webhookData = $webhookData;
45
        $this->tries = config('webhook.retries');
46
        $this->retryInterval = config('webhook.retry_interval');
47
    }
48
49
    /**
50
     * Process the webhook and dispatch it to the URL.
51
     */
52
    public function handle()
53
    {
54
        if (! $status = $this->sendViaHttp($this->webhookData)) {
55
            $this->retryWebhook();
56
        }
57
    }
58
59
    /**
60
     * Retry the webhook job.
61
     */
62
    private function retryWebhook()
63
    {
64
        ($this->attempts() < $this->tries)
65
            ? $this->release($this->retryInterval * 60)
66
            : $this->fail();
67
    }
68
}
69