Skip to content

Commit f1aebfa

Browse files
committed
Add project_id to subscriptions
1 parent f4e8d7b commit f1aebfa

4 files changed

Lines changed: 12 additions & 19 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ Load a specific subscription when you already know its id:
214214
$subscription = Pushpad\Subscription::find(123);
215215

216216
echo $subscription->id;
217+
echo $subscription->project_id;
217218
echo $subscription->endpoint;
218219
echo $subscription->uid;
219220
echo $subscription->tags;

lib/Pushpad.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class Pushpad
1313
{
1414
/** Current library version. */
15-
public const VERSION = '3.0.0';
15+
public const VERSION = '3.1.0';
1616

1717
/**
1818
* API token used to authenticate every request performed by the SDK.

lib/Subscription.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Subscription extends Resource
1111
{
1212
protected const ATTRIBUTES = [
1313
'id',
14+
'project_id',
1415
'endpoint',
1516
'p256dh',
1617
'auth',
@@ -23,6 +24,7 @@ class Subscription extends Resource
2324

2425
protected const READ_ONLY_ATTRIBUTES = [
2526
'id',
27+
'project_id',
2628
'last_click_at',
2729
'created_at',
2830
'project_id',
@@ -58,7 +60,7 @@ public static function findAll(array $query = [], ?int $projectId = null): array
5860
$items = $response['body'];
5961

6062
return array_map(
61-
fn (array $item) => new self(self::injectProjectId($item, $resolvedProjectId)),
63+
fn (array $item) => new self($item),
6264
$items
6365
);
6466
}
@@ -104,7 +106,7 @@ public static function find(int $subscriptionId, ?int $projectId = null): self
104106
self::ensureStatus($response, 200);
105107
$data = $response['body'];
106108

107-
return new self(self::injectProjectId($data, $resolvedProjectId));
109+
return new self($data);
108110
}
109111

110112
/**
@@ -124,7 +126,7 @@ public static function create(array $payload, ?int $projectId = null): self
124126
self::ensureStatus($response, 201);
125127
$data = $response['body'];
126128

127-
return new self(self::injectProjectId($data, $resolvedProjectId));
129+
return new self($data);
128130
}
129131

130132
/**
@@ -138,7 +140,7 @@ public function refresh(?int $projectId = null): self
138140
$response = self::httpGet("/projects/{$project}/subscriptions/{$this->requireId()}");
139141
self::ensureStatus($response, 200);
140142
$data = $response['body'];
141-
$this->setAttributes(self::injectProjectId($data, $project));
143+
$this->setAttributes($data);
142144
return $this;
143145
}
144146

@@ -158,7 +160,7 @@ public function update(array $payload, ?int $projectId = null): self
158160
]);
159161
self::ensureStatus($response, 200);
160162
$data = $response['body'];
161-
$this->setAttributes(self::injectProjectId($data, $project));
163+
$this->setAttributes($data);
162164
return $this;
163165
}
164166

@@ -181,28 +183,16 @@ public function delete(?int $projectId = null): void
181183
*/
182184
private function determineProjectId(?int $projectId = null): int
183185
{
184-
if ($projectId !== null) {
185-
return $projectId;
186-
}
187-
188186
if (isset($this->attributes['project_id'])) {
189187
return (int) $this->attributes['project_id'];
190188
}
191189

192-
return Pushpad::resolveProjectId(null);
190+
return Pushpad::resolveProjectId($projectId);
193191
}
194192

195193
/**
196194
* @param array<string, mixed> $data
197195
* @param int $projectId
198196
* @return array<string, mixed>
199197
*/
200-
private static function injectProjectId(array $data, int $projectId): array
201-
{
202-
if (!isset($data['project_id'])) {
203-
$data['project_id'] = $projectId;
204-
}
205-
206-
return $data;
207-
}
208198
}

tests/SubscriptionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public function testFindReturnsSubscription(): void
9090
'status' => 200,
9191
'body' => [
9292
'id' => 7,
93+
'project_id' => 321,
9394
'endpoint' => 'https://example.com/push/f7Q1Eyf',
9495
'uid' => 'user-123',
9596
'tags' => ['tag1'],
@@ -169,6 +170,7 @@ public function testUpdateSubscriptionSendsPayload(): void
169170
'status' => 200,
170171
'body' => [
171172
'id' => 7,
173+
'project_id' => 321,
172174
'uid' => 'user-updated',
173175
'endpoint' => 'https://example.com/push/f7Q1Eyf',
174176
],

0 commit comments

Comments
 (0)