Issues (57)

src/Webhooks/Dispatcher.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Slides\Connector\Auth\Webhooks;
4
5
use Illuminate\Support\Arr;
6
use Slides\Connector\Auth\Exceptions\WebhookException;
7
use Slides\Connector\Auth\Exceptions\WebhookValidationException;
8
use Slides\Connector\Auth\Concerns\WritesLogs;
9
use Slides\Connector\Auth\Contracts\Webhook as WebhookContract;
10
11
/**
12
 * Class Dispatcher
13
 *
14
 * @package Slides\Connector\Auth\Webhooks
15
 */
16
class Dispatcher
17
{
18
    use WritesLogs;
19
20
    /**
21
     * Handle a webhook.
22
     *
23
     * @param string $key
24
     * @param array $payload
25
     *
26
     * @return array|null
27
     *
28
     * @throws WebhookException
29
     * @throws WebhookValidationException
30
     */
31
    public function handle(string $key, array $payload)
32
    {
33
        if(!$this->has($key)) {
34
            throw new WebhookException("Webhook with key \"{$key}\" cannot be found.");
35
        }
36
37
        $webhook = $this->instantiate($key);
38
39
        if(!$webhook->validate($payload)) {
0 ignored issues
show
The method validate() does not exist on Slides\Connector\Auth\Contracts\Webhook. Since it exists in all sub-types, consider adding an abstract or default implementation to Slides\Connector\Auth\Contracts\Webhook. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        if(!$webhook->/** @scrutinizer ignore-call */ validate($payload)) {
Loading history...
40
            throw new WebhookValidationException($webhook->getValidator());
0 ignored issues
show
The method getValidator() does not exist on Slides\Connector\Auth\Contracts\Webhook. Since it exists in all sub-types, consider adding an abstract or default implementation to Slides\Connector\Auth\Contracts\Webhook. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            throw new WebhookValidationException($webhook->/** @scrutinizer ignore-call */ getValidator());
Loading history...
41
        }
42
43
        try {
44
            return $webhook->handle($payload);
45
        }
46
        catch(\Exception $e) {
47
            throw new WebhookException(get_class($webhook) . ': ' . $e->getMessage());
48
        }
49
    }
50
51
    /**
52
     * Instantiate a webhook handler.
53
     *
54
     * @param string $key
55
     *
56
     * @return Webhook|WebhookContract
57
     */
58
    private function instantiate(string $key)
59
    {
60
        $webhook = $this->get($key);
61
62
        return app($webhook);
63
    }
64
65
    /**
66
     * Check whether a given webhook exists.
67
     *
68
     * @param string $key
69
     *
70
     * @return bool
71
     */
72
    private function has(string $key): bool
73
    {
74
        return array_key_exists($key, static::webhooks());
75
    }
76
77
    /**
78
     * Retrieve a class handler of by the webhook key.
79
     *
80
     * @param string $key
81
     * @param string|null $default
82
     *
83
     * @return string|null
84
     */
85
    private function get(string $key, $default = null)
86
    {
87
        return Arr::get(static::webhooks(), $key, $default);
88
    }
89
90
    /**
91
     * The supported webhook identifiers.
92
     *
93
     * @return array
94
     */
95
    public static function webhooks()
96
    {
97
        return [
98
            'user.sync' => UserSyncWebhook::class,
99
            'assess.users' => AssessUsersWebhook::class
100
        ];
101
    }
102
}