Passed
Push — master ( 00facb...0799ad )
by Raza
01:27
created

WebHooks::webhooks_update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Srmklive\Chargify\Traits\ChargifyAPI;
4
5
use Carbon\Carbon;
6
7
trait WebHooks
8
{
9
    /**
10
     * List web hooks for a site.
11
     *
12
     * @param string $start
13
     * @param string $end
14
     * @param bool   $newest
15
     * @param int    $page
16
     * @param int    $per_page
17
     * @param int    $subscription_id
18
     *
19
     * @return array
20
     */
21
    public function webhooks_list($start = '', $end = '', $newest = true, $page = 1, $per_page = 200, $subscription_id = 0) : array
22
    {
23
        $now = Carbon::now();
24
25
        $start_date = empty($start) ? $now->subDays(15)->toDateString() : $start;
26
        $end_date = empty($end) ? $now->toDateString() : $end;
27
        $order = ($newest === true) ? 'newest_first' : 'oldest_first';
28
        $subscription_field = $subscription_id > 0 ? "subscription_id={$subscription_id}&" : '';
29
30
        $this->apiEndPoint = "/webhooks.json?{$subscription_field}page={$page}&per_page={$per_page}&since_date={$start_date}&until_date={$end_date}&order={$order}";
31
32
        $this->verb = 'get';
33
34
        return $this->doChargifyRequest();
0 ignored issues
show
Bug introduced by
It seems like doChargifyRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

34
        return $this->/** @scrutinizer ignore-call */ doChargifyRequest();
Loading history...
35
    }
36
37
    /**
38
     * Replay web hooks for the site.
39
     *
40
     * @param array $webhook_ids
41
     *
42
     * @return array
43
     */
44
    public function webhooks_replay(array $webhook_ids): array
45
    {
46
        $this->apiEndPoint = "/webhooks/replay.json";
47
48
        $this->options['json'] = ['ids' => $webhook_ids];
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
50
        $this->verb = 'post';
51
52
        return $this->doChargifyRequest();
53
    }
54
55
    /**
56
     * Create a new web hook for the site.
57
     *
58
     * @param string $url
59
     * @param array  $events
60
     *
61
     * @return array
62
     */
63
    public function webhooks_create(string $url, array $events = []): array
64
    {
65
        $this->apiEndPoint = "/endpoints.json";
66
67
        $this->options['json'] = collect([
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
68
            'endpoint' => [
69
                'url'                   => $url,
70
                'webhook_subscriptions' => $events,
71
            ],
72
        ])->toArray();
73
74
        $this->verb = 'post';
75
76
        return $this->doChargifyRequest();
77
    }
78
79
    /**
80
     * Update a new web hook for the site.
81
     *
82
     * @param string $webhook_id
83
     * @param string $url
84
     * @param array  $events
85
     *
86
     * @return array
87
     */
88
    public function webhooks_update(string $webhook_id, string $url, array $events = []): array
89
    {
90
        $this->apiEndPoint = "/endpoints/{$webhook_id}.json";
91
92
        $this->options['json'] = collect([
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
93
            'endpoint' => [
94
                'url'                   => $url,
95
                'webhook_subscriptions' => $events,
96
            ],
97
        ])->toArray();
98
99
        $this->verb = 'put';
100
101
        return $this->doChargifyRequest();
102
    }
103
104
    /**
105
     * Get web hook endpoints for the site.
106
     *
107
     * @return array
108
     */
109
    public function webhooks_endpoints(): array
110
    {
111
        $this->apiEndPoint = "/endpoints.json";
112
113
        $this->verb = 'get';
114
115
        return $this->doChargifyRequest();
116
    }
117
118
    /**
119
     * Enable web hooks for the site.
120
     *
121
     * @return array
122
     */
123
    public function webhooks_enable(): array
124
    {
125
        $this->apiEndPoint = "/webhooks/settings.json";
126
127
        $this->options['json'] = [
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
128
            'webhooks_enabled' => true,
129
        ];
130
        $this->verb = 'put';
131
132
        return $this->doChargifyRequest();
133
    }
134
}
135