Skip to content

Commit

Permalink
Added Event Listener system (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid authored Nov 21, 2023
1 parent 6284982 commit 0f24b0c
Show file tree
Hide file tree
Showing 25 changed files with 467 additions and 19 deletions.
8 changes: 8 additions & 0 deletions .idea/laravel-shopify.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<directory>src/Exceptions/</directory>
<directory>src/Objects/Enums/</directory>
<directory>src/resources/</directory>
<file>src/Messaging/Events/AppLoggedIn.php</file>
<directory>src/Messaging/Events/</directory>
<file>src/ShopifyAppProvider.php</file>
</exclude>
<report>
Expand Down
3 changes: 3 additions & 0 deletions src/Actions/ActivatePlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Osiset\ShopifyApp\Contracts\Objects\Values\PlanId;
use Osiset\ShopifyApp\Contracts\Queries\Plan as IPlanQuery;
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
use Osiset\ShopifyApp\Messaging\Events\PlanActivatedEvent;
use Osiset\ShopifyApp\Objects\Enums\ChargeStatus;
use Osiset\ShopifyApp\Objects\Enums\ChargeType;
use Osiset\ShopifyApp\Objects\Enums\PlanType;
Expand Down Expand Up @@ -142,6 +143,8 @@ public function __invoke(ShopId $shopId, PlanId $planId, ChargeReference $charge
$charge = $this->chargeCommand->make($transfer);
$this->shopCommand->setToPlan($shopId, $planId);

event(new PlanActivatedEvent($shop, $plan, $charge));

return $charge;
}
}
3 changes: 3 additions & 0 deletions src/Actions/AuthenticateShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Osiset\ShopifyApp\Contracts\ApiHelper as IApiHelper;
use Osiset\ShopifyApp\Messaging\Events\AppInstalledEvent;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Util;

Expand Down Expand Up @@ -109,6 +110,8 @@ public function __invoke(Request $request): array
call_user_func($this->dispatchWebhooksAction, $result['shop_id'], false);
call_user_func($this->afterAuthorizeAction, $result['shop_id']);

event(new AppInstalledEvent($result['shop_id']));

return [$result, true];
}
}
10 changes: 9 additions & 1 deletion src/Http/Middleware/IframeProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Cache;
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Util;

/**
* Responsibility for protection against clickjaking
Expand Down Expand Up @@ -44,6 +45,7 @@ public function __construct(
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$ancestors = Util::getShopifyConfig('iframe_ancestors');

$shop = Cache::remember(
'frame-ancestors_'.$request->get('shop'),
Expand All @@ -57,9 +59,15 @@ function () use ($request) {
? $shop->name
: '*.myshopify.com';

$iframeAncestors = "frame-ancestors https://$domain https://admin.shopify.com";

if (!blank($ancestors)) {
$iframeAncestors .= ' '.$ancestors;
}

$response->headers->set(
'Content-Security-Policy',
"frame-ancestors https://$domain https://admin.shopify.com"
$iframeAncestors
);

return $response;
Expand Down
39 changes: 37 additions & 2 deletions src/Http/Middleware/VerifyShopify.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(
* @param Request $request The request object.
* @param Closure $next The next action.
*
* @throws SignatureVerificationException If HMAC verification fails.
* @throws SignatureVerificationException|HttpException If HMAC verification fails.
*
* @return mixed
*/
Expand All @@ -102,9 +102,12 @@ public function handle(Request $request, Closure $next)
}

if (!Util::useNativeAppBridge()) {
$storeResult = !$this->isApiRequest($request) && $this->checkPreviousInstallation($request);
$shop = $this->getShopIfAlreadyInstalled($request);
$storeResult = !$this->isApiRequest($request) && $shop;

if ($storeResult) {
$this->loginFromShop($shop);

return $next($request);
}
}
Expand Down Expand Up @@ -512,4 +515,36 @@ protected function checkPreviousInstallation(Request $request): bool

return $shop && $shop->password && ! $shop->trashed();
}

/**
* Get shop model if there is a store record in the database.
*
* @param Request $request The request object.
*
* @return ?ShopModel
*/
protected function getShopIfAlreadyInstalled(Request $request): ?ShopModel
{
$shop = $this->shopQuery->getByDomain(ShopDomain::fromRequest($request), [], true);

return $shop && $shop->password && ! $shop->trashed() ? $shop : null;
}

/**
* Login and validate store
*
* @param ShopModel $shop
*
* @return void
*/
protected function loginFromShop(ShopModel $shop): void
{
// Override auth guard
if (($guard = Util::getShopifyConfig('shop_auth_guard'))) {
$this->auth->setDefaultDriver($guard);
}

// All is well, login the shop
$this->auth->login($shop);
}
}
35 changes: 35 additions & 0 deletions src/Messaging/Events/AppInstalledEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Osiset\ShopifyApp\Messaging\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Objects\Values\ShopId;

/**
* Event fired when this
*/
class AppInstalledEvent
{
use Dispatchable;
use SerializesModels;

/**
* Shop's instance.
*
* @var ShopId
*/
public $shopId;

/**
* Create a new event instance.
*
* @param ShopId $shop_id
*
* @return void
*/
public function __construct(ShopId $shop_id)
{
$this->shopId = $shop_id;
}
}
35 changes: 35 additions & 0 deletions src/Messaging/Events/AppUninstalledEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Osiset\ShopifyApp\Messaging\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;

/**
* Event fired when this
*/
class AppUninstalledEvent
{
use Dispatchable;
use SerializesModels;

/**
* Shop's instance.
*
* @var IShopModel
*/
public $shop;

/**
* Create a new event instance.
*
* @param IShopModel $shop
*
* @return void
*/
public function __construct(IShopModel $shop)
{
$this->shop = $shop;
}
}
53 changes: 53 additions & 0 deletions src/Messaging/Events/PlanActivatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Osiset\ShopifyApp\Messaging\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;
use Osiset\ShopifyApp\Objects\Values\ChargeId;

/**
* Event fired when this
*/
class PlanActivatedEvent
{
use Dispatchable;
use SerializesModels;

/**
* Shop's instance.
*
* @var IShopModel
*/
public $shop;

/**
* Plan's instance.
*
* @var Model
*/
public $plan;

/**
* Charge ID
*
* @var ChargeId
*/
public $chargeId;

/**
* Create a new event instance.
*
* @param IShopModel $shop
*
* @return void
*/
public function __construct(IShopModel $shop, Model $plan, ChargeId $chargeId)
{
$this->shop = $shop;
$this->plan = $plan;
$this->chargeId = $chargeId;
}
}
35 changes: 35 additions & 0 deletions src/Messaging/Events/ShopAuthenticatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Osiset\ShopifyApp\Messaging\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Osiset\ShopifyApp\Objects\Values\ShopId;

/**
* Event fired when this
*/
class ShopAuthenticatedEvent
{
use Dispatchable;
use SerializesModels;

/**
* Shop's instance.
*
* @var ShopId
*/
public $shopId;

/**
* Create a new event instance.
*
* @param ShopId $shop_id
*
* @return void
*/
public function __construct(ShopId $shopId)
{
$this->shopId = $shopId;
}
}
Loading

0 comments on commit 0f24b0c

Please sign in to comment.