BaseWebhook::generatePayload()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Bencoderus\Webhook\Webhooks;
4
5
use Bencoderus\Webhook\Exceptions\WebhookException;
6
use Bencoderus\Webhook\Traits\SendWebhook;
7
use Illuminate\Http\Resources\DelegatesToResource;
8
use Illuminate\Support\Str;
9
10
abstract class BaseWebhook
11
{
12
    use SendWebhook, DelegatesToResource;
13
14
    /**
15
     * Webhook encryption signature.
16
     *
17
     * @var array
18
     */
19
    public $signature;
20
21
    /**
22
     * The URL the outgoing will be dispatched to.
23
     *
24
     * @var string
25
     */
26
    private $url;
27
28
    /**
29
     * The data that will be dispatched with the outgoing webhook.
30
     *
31
     * @var array
32
     */
33
    private $payload;
34
35
    /**
36
     * An instance of the webhook payload.
37
     *
38
     * @var null
39
     */
40
    private $resource;
41
42
    /**
43
     * Create a webhook instance.
44
     *
45
     * @param null $model
46
     */
47
    public function __construct($model = null)
48
    {
49
        $this->resource = $model;
50
    }
51
52
    /**
53
     * Add a webhook signature.
54
     *
55
     * @param string $signatureName
56
     * @param string $encryptionKey
57
     * @param string $hashAlgorithm
58
     *
59
     * @return $this
60
     */
61
    public function withSignature(string $signatureName, string $encryptionKey, string $hashAlgorithm = 'sha512'): self
62
    {
63
        $encryptionKey = hash($hashAlgorithm, $encryptionKey);
64
65
        $this->signature = [$signatureName => $encryptionKey];
66
67
        return $this;
68
    }
69
70
    /**
71
     * Add webhook url.
72
     *
73
     * @param string $url
74
     *
75
     * @return $this
76
     */
77
    public function url(string $url): self
78
    {
79
        $this->url = $url;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Send webhook asynchronously(Queue) or synchronously.
86
     *
87
     * @param bool $sendWithQueue
88
     *
89
     * @return bool
90
     * @throws \Bencoderus\Webhook\Exceptions\WebhookException
91
     */
92
    public function send(bool $sendWithQueue = true): bool
93
    {
94
        $data = $this->prepareWebhook();
95
96
        if (empty($data['url'])) {
97
            throw new WebhookException("You need to set a webhook URL");
98
        }
99
100
        if (! config('webhook.dispatch_webhook')) {
101
            return true;
102
        }
103
104
        if ($sendWithQueue) {
105
            return $this->sendViaQueue($data);
106
        }
107
108
        return $this->sendViaHttp($data);
109
    }
110
111
    /**
112
     * Generate webhook data.
113
     *
114
     * @return array
115
     */
116
    public function prepareWebhook(): array
117
    {
118
        $webhookData = [];
119
120
        $this->generatePayload();
121
122
        if ($this->signature) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->signature of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
123
            $webhookData['signature'] = $this->signature ?? [];
124
        }
125
126
        $webhookData['url'] = $this->url;
127
        $webhookData['payload'] = $this->payload;
128
        $webhookData['webhook_id'] = Str::uuid();
129
130
        return $webhookData;
131
    }
132
133
    /**
134
     * Format outgoing webhook payload
135
     *
136
     * @return array
137
     */
138
    private function generatePayload(): array
139
    {
140
        $this->payload = array_merge(
141
            ['event' => $this->event],
0 ignored issues
show
Documentation introduced by
The property event does not exist on object<Bencoderus\Webhook\Webhooks\BaseWebhook>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
142
            ['data' => $this->data()]
143
        );
144
145
        return $this->payload;
146
    }
147
148
    /**
149
     * Webhook payload data.
150
     *
151
     * @return mixed
152
     */
153
    abstract public function data();
154
155
}
156