Skip to content

Commit 3ed5b58

Browse files
author
Tyler King
authored
Fix for Job Serialization for Webhooks (gnikyt#624)
* Fix to remove job class from arguments passed to job, issues with serialization occurs * StyleCI fixes * Adjustment to webhook jobs and webhook stub to use scalar values for job arguments * Fix to StyleCI
1 parent c992cee commit 3ed5b58

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

src/ShopifyApp/Console/stubs/webhook-job.stub

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class DummyClass implements ShouldQueue
1515
/**
1616
* Shop's myshopify domain
1717
*
18-
* @var ShopDomain
18+
* @var ShopDomain|string
1919
*/
2020
public $shopDomain;
2121

@@ -29,8 +29,8 @@ class DummyClass implements ShouldQueue
2929
/**
3030
* Create a new job instance.
3131
*
32-
* @param ShopDomain $shopDomain The shop's myshopify domain
33-
* @param stdClass $data The webhook data (JSON decoded)
32+
* @param string $shopDomain The shop's myshopify domain.
33+
* @param stdClass $data The webhook data (JSON decoded).
3434
*
3535
* @return void
3636
*/
@@ -47,6 +47,9 @@ class DummyClass implements ShouldQueue
4747
*/
4848
public function handle()
4949
{
50+
// Convert domain
51+
$this->shopDomain = ShopDomain::fromNative($this->domain);
52+
5053
// Do what you wish with the data
5154
// Access domain name as $this->shopDomain->toNative()
5255
}

src/ShopifyApp/Messaging/Jobs/AppUninstalledJob.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Illuminate\Queue\SerializesModels;
1010
use Osiset\ShopifyApp\Actions\CancelCurrentPlan;
1111
use Osiset\ShopifyApp\Contracts\Commands\Shop as IShopCommand;
12-
use Osiset\ShopifyApp\Contracts\Objects\Values\ShopDomain;
1312
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
13+
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
1414
use stdClass;
1515

1616
/**
@@ -26,7 +26,7 @@ class AppUninstalledJob implements ShouldQueue
2626
/**
2727
* The shop domain.
2828
*
29-
* @var ShopDomain
29+
* @var ShopDomain|string
3030
*/
3131
protected $domain;
3232

@@ -40,12 +40,12 @@ class AppUninstalledJob implements ShouldQueue
4040
/**
4141
* Create a new job instance.
4242
*
43-
* @param ShopDomain $domain The shop domain.
44-
* @param stdClass $data The webhook data (JSON decoded).
43+
* @param string $domain The shop domain.
44+
* @param stdClass $data The webhook data (JSON decoded).
4545
*
4646
* @return void
4747
*/
48-
public function __construct(ShopDomain $domain, stdClass $data)
48+
public function __construct(string $domain, stdClass $data)
4949
{
5050
$this->domain = $domain;
5151
$this->data = $data;
@@ -65,6 +65,9 @@ public function handle(
6565
IShopQuery $shopQuery,
6666
CancelCurrentPlan $cancelCurrentPlanAction
6767
): bool {
68+
// Convert the domain
69+
$this->domain = ShopDomain::fromNative($this->domain);
70+
6871
// Get the shop
6972
$shop = $shopQuery->getByDomain($this->domain);
7073
$shopId = $shop->getId();

src/ShopifyApp/Traits/WebhookController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Http\Response as ResponseResponse;
77
use Illuminate\Support\Facades\Response;
8-
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
98

109
/**
1110
* Responsible for handling incoming webhook requests.
@@ -29,7 +28,7 @@ public function handle(string $type, Request $request): ResponseResponse
2928
$jobData = json_decode($request->getContent());
3029

3130
$jobClass::dispatch(
32-
ShopDomain::fromNative($request->header('x-shopify-shop-domain')),
31+
$request->header('x-shopify-shop-domain'),
3332
$jobData
3433
);
3534

tests/Messaging/Jobs/AppUninstalledTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testJobSoftDeletesShopAndCharges()
3838

3939
// Run the job
4040
AppUninstalledJob::dispatchNow(
41-
$shop->getDomain(),
41+
$shop->getDomain()->toNative(),
4242
json_decode(file_get_contents(__DIR__.'/../../fixtures/app_uninstalled.json'))
4343
);
4444

tests/Traits/WebhookControllerTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Osiset\ShopifyApp\Test\Controllers;
44

55
use Illuminate\Support\Facades\Queue;
6+
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
67
use Osiset\ShopifyApp\Test\TestCase;
78

89
require_once __DIR__.'/../Stubs/OrdersCreateJob.php';
@@ -36,9 +37,9 @@ public function testSuccess(): void
3637
// Check it was created and job was pushed
3738
$response->assertStatus(201);
3839
Queue::assertPushed(\App\Jobs\OrdersCreateJob::class, function ($job) use ($shop) {
39-
return $job->shopDomain->isSame($shop->getDomain())
40-
&& $job->data instanceof \stdClass
41-
&& $job->data->email === 'jon@doe.ca';
40+
return ShopDomain::fromNative($job->shopDomain)->isSame($shop->getDomain())
41+
&& $job->data instanceof \stdClass
42+
&& $job->data->email === 'jon@doe.ca';
4243
});
4344
}
4445

0 commit comments

Comments
 (0)