Skip to content

Commit

Permalink
Fix for Job Serialization for Webhooks (gnikyt#624)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gnikyt authored Nov 25, 2020
1 parent c992cee commit 3ed5b58
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/ShopifyApp/Console/stubs/webhook-job.stub
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DummyClass implements ShouldQueue
/**
* Shop's myshopify domain
*
* @var ShopDomain
* @var ShopDomain|string
*/
public $shopDomain;

Expand All @@ -29,8 +29,8 @@ class DummyClass implements ShouldQueue
/**
* Create a new job instance.
*
* @param ShopDomain $shopDomain The shop's myshopify domain
* @param stdClass $data The webhook data (JSON decoded)
* @param string $shopDomain The shop's myshopify domain.
* @param stdClass $data The webhook data (JSON decoded).
*
* @return void
*/
Expand All @@ -47,6 +47,9 @@ class DummyClass implements ShouldQueue
*/
public function handle()
{
// Convert domain
$this->shopDomain = ShopDomain::fromNative($this->domain);

// Do what you wish with the data
// Access domain name as $this->shopDomain->toNative()
}
Expand Down
13 changes: 8 additions & 5 deletions src/ShopifyApp/Messaging/Jobs/AppUninstalledJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Actions\CancelCurrentPlan;
use Osiset\ShopifyApp\Contracts\Commands\Shop as IShopCommand;
use Osiset\ShopifyApp\Contracts\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use stdClass;

/**
Expand All @@ -26,7 +26,7 @@ class AppUninstalledJob implements ShouldQueue
/**
* The shop domain.
*
* @var ShopDomain
* @var ShopDomain|string
*/
protected $domain;

Expand All @@ -40,12 +40,12 @@ class AppUninstalledJob implements ShouldQueue
/**
* Create a new job instance.
*
* @param ShopDomain $domain The shop domain.
* @param stdClass $data The webhook data (JSON decoded).
* @param string $domain The shop domain.
* @param stdClass $data The webhook data (JSON decoded).
*
* @return void
*/
public function __construct(ShopDomain $domain, stdClass $data)
public function __construct(string $domain, stdClass $data)
{
$this->domain = $domain;
$this->data = $data;
Expand All @@ -65,6 +65,9 @@ public function handle(
IShopQuery $shopQuery,
CancelCurrentPlan $cancelCurrentPlanAction
): bool {
// Convert the domain
$this->domain = ShopDomain::fromNative($this->domain);

// Get the shop
$shop = $shopQuery->getByDomain($this->domain);
$shopId = $shop->getId();
Expand Down
3 changes: 1 addition & 2 deletions src/ShopifyApp/Traits/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response as ResponseResponse;
use Illuminate\Support\Facades\Response;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;

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

$jobClass::dispatch(
ShopDomain::fromNative($request->header('x-shopify-shop-domain')),
$request->header('x-shopify-shop-domain'),
$jobData
);

Expand Down
2 changes: 1 addition & 1 deletion tests/Messaging/Jobs/AppUninstalledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testJobSoftDeletesShopAndCharges()

// Run the job
AppUninstalledJob::dispatchNow(
$shop->getDomain(),
$shop->getDomain()->toNative(),
json_decode(file_get_contents(__DIR__.'/../../fixtures/app_uninstalled.json'))
);

Expand Down
7 changes: 4 additions & 3 deletions tests/Traits/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Osiset\ShopifyApp\Test\Controllers;

use Illuminate\Support\Facades\Queue;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Test\TestCase;

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

Expand Down

0 comments on commit 3ed5b58

Please sign in to comment.