• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NIT-Administrative-Systems / SysDev-laravel-soa / 6383158570

02 Oct 2023 05:01PM UTC coverage: 46.154%. Remained the same
6383158570

push

github

Nick Evans
Always save the topicName when making a webhook

3 of 3 new or added lines in 1 file covered. (100.0%)

264 of 572 relevant lines covered (46.15%)

18.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Console/Commands/EventHub/WebhookConfiguration.php
1
<?php
2

3
namespace Northwestern\SysDev\SOA\Console\Commands\EventHub;
4

5
use Illuminate\Console\Command;
6
use Northwestern\SysDev\SOA\EventHub;
7
use Illuminate\Console\ConfirmableTrait;
8
use Northwestern\SysDev\SOA\Routing\EventHubWebhookRegistration;
9

10
class WebhookConfiguration extends Command
11
{
12
    use ConfirmableTrait;
13

14
    protected $signature = 'eventhub:webhook:configure {--force}';
15
    protected $description = 'Configure webhook routes in EventHub';
16

17
    protected $webhook_api;
18
    protected $hook_registry;
19

20
    public function __construct(EventHub\Webhook $webhook_api, EventHubWebhookRegistration $hook_registry)
21
    {
22
        parent::__construct();
×
23

24
        $this->webhook_api = $webhook_api;
×
25
        $this->hook_registry = $hook_registry;
×
26
    } // end __construct
27

28
    public function handle()
29
    {
30
        $force_delete = $this->option('force');
×
31

32
        $registered_hooks = collect($this->webhook_api->listAll()['webhooks']);
×
33
        $registered_hooks = $registered_hooks->map(function ($hook) {
×
34
            return $hook['topicName'];
×
35
        })->all();
×
36

37
        // We can remove stuff from this one as we touch them -- that we we know which still exist.
38
        $existing_hooks_updated = $registered_hooks;
×
39

40
        $desired_hooks = $this->hook_registry->getHooks();
×
41
        foreach ($desired_hooks as $hook) {
×
42
            $hook = $hook->toArray();
×
43

44
            // Remove from unmanaged list
45
            $existing_hooks_updated = array_diff($existing_hooks_updated, [$hook['topicName']]);
×
46

47
            $this->createOrUpdate($hook, $registered_hooks);
×
48
        }
49

50
        if (sizeof($existing_hooks_updated) > 0) {
×
51
            $this->line('');
×
52
            $this->error('The following webhooks are configured in EventHub but do not have a corresponding entry in the routes file:');
×
53
            $this->line('');
×
54
            $this->error(implode(', ', $existing_hooks_updated));
×
55
            $this->line('');
×
56

57
            $delete_unmanaged = $force_delete ?: $this->confirm('Would you like to delete these unmanaged webhooks?');
×
58
            if ($delete_unmanaged === true) {
×
59
                foreach ($existing_hooks_updated as $hook_to_delete) {
×
60
                    $this->delete($hook_to_delete);
×
61
                }
62
            }
63
        }
64

65
        // Display status after the changes are made.
66
        $this->call('eventhub:webhook:status');
×
67

68
        return 0;
×
69
    } // end handle
70

71
    protected function createOrUpdate($hook, $registered_hooks)
72
    {
73
        $topicName = $hook['topicName'];
×
74

75
        try {
76
            // If the hook exists on EventHub already, we don't need to touch the 'active' status.
77
            // But, new ones will require it.
78
            if (in_array($hook['topicName'], $registered_hooks) === false) {
×
79
                // Not permitted in creates since Jan 2022 (but fine for updates)
80
                unset($hook['topicName']);
×
81

82
                $hook['active'] = true;
×
83

84
                $this->webhook_api->create($topicName, $hook);
×
85
            } else {
86
                $this->webhook_api->updateConfig($topicName, $hook);
×
87
            }
88
        } catch (EventHub\Exception\EventHubError $e) {
×
89
            $this->line('');
×
90
            $this->error(vsprintf('Failed to update %s: %s', [$topicName, $e->getMessage()]));
×
91
            $this->line('');
×
92
        }
93
    } // end createOrUpdate
94

95
    protected function delete($queue_name)
96
    {
97
        try {
98
            $this->webhook_api->delete($queue_name);
×
99
        } catch (EventHub\Exception\EventHubError $e) {
×
100
            $this->line('');
×
101
            $this->error(vsprintf('Failed to delete %s: %s', [$queue_name, $e->getMessage()]));
×
102
            $this->line('');
×
103
        }
104
    } // end delete
105
} // end WebhookConfiguration
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc