WebHooks   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 129
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A updateWebHook() 0 9 1
A listWebHookEvents() 0 7 1
A listWebHooks() 0 7 1
A createWebHook() 0 14 1
A deleteWebHook() 0 7 1
A showWebHookDetails() 0 7 1
1
<?php
2
3
namespace Srmklive\PayPal\Traits\PayPalAPI;
4
5
trait WebHooks
6
{
7
    /**
8
     * Create a new web hook.
9
     *
10
     * @param string $url
11
     * @param array  $events
12
     *
13
     * @throws \Throwable
14
     *
15
     * @return array|\Psr\Http\Message\StreamInterface|string
16
     *
17
     * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_post
18
     */
19
    public function createWebHook(string $url, array $events)
20
    {
21
        $this->apiEndPoint = 'v1/notifications/webhooks';
22
23
        $data = ['url' => $url];
24
        $data['event_types'] = collect($events)->map(function ($item) {
0 ignored issues
show
Bug introduced by
$events of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

24
        $data['event_types'] = collect(/** @scrutinizer ignore-type */ $events)->map(function ($item) {
Loading history...
25
            return ['name' => $item];
26
        })->toArray();
27
28
        $this->options['json'] = $data;
29
30
        $this->verb = 'post';
31
32
        return $this->doPayPalRequest();
33
    }
34
35
    /**
36
     * List all web hooks.
37
     *
38
     * @throws \Throwable
39
     *
40
     * @return array|\Psr\Http\Message\StreamInterface|string
41
     *
42
     * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_list
43
     */
44
    public function listWebHooks()
45
    {
46
        $this->apiEndPoint = 'v1/notifications/webhooks';
47
48
        $this->verb = 'get';
49
50
        return $this->doPayPalRequest();
51
    }
52
53
    /**
54
     * Delete a web hook.
55
     *
56
     * @param string $web_hook_id
57
     *
58
     * @throws \Throwable
59
     *
60
     * @return array|\Psr\Http\Message\StreamInterface|string
61
     *
62
     * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_delete
63
     */
64
    public function deleteWebHook(string $web_hook_id)
65
    {
66
        $this->apiEndPoint = "v1/notifications/webhooks/{$web_hook_id}";
67
68
        $this->verb = 'delete';
69
70
        return $this->doPayPalRequest(false);
71
    }
72
73
    /**
74
     * Update an existing web hook.
75
     *
76
     * @param string $web_hook_id
77
     * @param array  $data
78
     *
79
     * @throws \Throwable
80
     *
81
     * @return array|\Psr\Http\Message\StreamInterface|string
82
     *
83
     * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_update
84
     */
85
    public function updateWebHook(string $web_hook_id, array $data)
86
    {
87
        $this->apiEndPoint = "v1/notifications/webhooks/{$web_hook_id}";
88
89
        $this->options['json'] = $data;
90
91
        $this->verb = 'patch';
92
93
        return $this->doPayPalRequest();
94
    }
95
96
    /**
97
     * Show details for an existing web hook.
98
     *
99
     * @param string $web_hook_id
100
     *
101
     * @throws \Throwable
102
     *
103
     * @return array|\Psr\Http\Message\StreamInterface|string
104
     *
105
     * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_get
106
     */
107
    public function showWebHookDetails(string $web_hook_id)
108
    {
109
        $this->apiEndPoint = "v1/notifications/webhooks/{$web_hook_id}";
110
111
        $this->verb = 'get';
112
113
        return $this->doPayPalRequest();
114
    }
115
116
    /**
117
     * List events for an existing web hook.
118
     *
119
     * @param string $web_hook_id
120
     *
121
     * @throws \Throwable
122
     *
123
     * @return array|\Psr\Http\Message\StreamInterface|string
124
     *
125
     * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_get
126
     */
127
    public function listWebHookEvents($web_hook_id)
128
    {
129
        $this->apiEndPoint = "v1/notifications/webhooks/{$web_hook_id}/event-types";
130
131
        $this->verb = 'get';
132
133
        return $this->doPayPalRequest();
134
    }
135
}
136