Skip to content

Commit 0f24b0c

Browse files
authored
Added Event Listener system (#212)
1 parent 6284982 commit 0f24b0c

25 files changed

+467
-19
lines changed

.idea/laravel-shopify.iml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

+28-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<directory>src/Exceptions/</directory>
2424
<directory>src/Objects/Enums/</directory>
2525
<directory>src/resources/</directory>
26-
<file>src/Messaging/Events/AppLoggedIn.php</file>
26+
<directory>src/Messaging/Events/</directory>
2727
<file>src/ShopifyAppProvider.php</file>
2828
</exclude>
2929
<report>

src/Actions/ActivatePlan.php

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Osiset\ShopifyApp\Contracts\Objects\Values\PlanId;
99
use Osiset\ShopifyApp\Contracts\Queries\Plan as IPlanQuery;
1010
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
11+
use Osiset\ShopifyApp\Messaging\Events\PlanActivatedEvent;
1112
use Osiset\ShopifyApp\Objects\Enums\ChargeStatus;
1213
use Osiset\ShopifyApp\Objects\Enums\ChargeType;
1314
use Osiset\ShopifyApp\Objects\Enums\PlanType;
@@ -142,6 +143,8 @@ public function __invoke(ShopId $shopId, PlanId $planId, ChargeReference $charge
142143
$charge = $this->chargeCommand->make($transfer);
143144
$this->shopCommand->setToPlan($shopId, $planId);
144145

146+
event(new PlanActivatedEvent($shop, $plan, $charge));
147+
145148
return $charge;
146149
}
147150
}

src/Actions/AuthenticateShop.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\Request;
66
use Osiset\ShopifyApp\Contracts\ApiHelper as IApiHelper;
7+
use Osiset\ShopifyApp\Messaging\Events\AppInstalledEvent;
78
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
89
use Osiset\ShopifyApp\Util;
910

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

113+
event(new AppInstalledEvent($result['shop_id']));
114+
112115
return [$result, true];
113116
}
114117
}

src/Http/Middleware/IframeProtection.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\Cache;
88
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
99
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
10+
use Osiset\ShopifyApp\Util;
1011

1112
/**
1213
* Responsibility for protection against clickjaking
@@ -44,6 +45,7 @@ public function __construct(
4445
public function handle(Request $request, Closure $next)
4546
{
4647
$response = $next($request);
48+
$ancestors = Util::getShopifyConfig('iframe_ancestors');
4749

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

62+
$iframeAncestors = "frame-ancestors https://$domain https://admin.shopify.com";
63+
64+
if (!blank($ancestors)) {
65+
$iframeAncestors .= ' '.$ancestors;
66+
}
67+
6068
$response->headers->set(
6169
'Content-Security-Policy',
62-
"frame-ancestors https://$domain https://admin.shopify.com"
70+
$iframeAncestors
6371
);
6472

6573
return $response;

src/Http/Middleware/VerifyShopify.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function __construct(
8383
* @param Request $request The request object.
8484
* @param Closure $next The next action.
8585
*
86-
* @throws SignatureVerificationException If HMAC verification fails.
86+
* @throws SignatureVerificationException|HttpException If HMAC verification fails.
8787
*
8888
* @return mixed
8989
*/
@@ -102,9 +102,12 @@ public function handle(Request $request, Closure $next)
102102
}
103103

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

107108
if ($storeResult) {
109+
$this->loginFromShop($shop);
110+
108111
return $next($request);
109112
}
110113
}
@@ -512,4 +515,36 @@ protected function checkPreviousInstallation(Request $request): bool
512515

513516
return $shop && $shop->password && ! $shop->trashed();
514517
}
518+
519+
/**
520+
* Get shop model if there is a store record in the database.
521+
*
522+
* @param Request $request The request object.
523+
*
524+
* @return ?ShopModel
525+
*/
526+
protected function getShopIfAlreadyInstalled(Request $request): ?ShopModel
527+
{
528+
$shop = $this->shopQuery->getByDomain(ShopDomain::fromRequest($request), [], true);
529+
530+
return $shop && $shop->password && ! $shop->trashed() ? $shop : null;
531+
}
532+
533+
/**
534+
* Login and validate store
535+
*
536+
* @param ShopModel $shop
537+
*
538+
* @return void
539+
*/
540+
protected function loginFromShop(ShopModel $shop): void
541+
{
542+
// Override auth guard
543+
if (($guard = Util::getShopifyConfig('shop_auth_guard'))) {
544+
$this->auth->setDefaultDriver($guard);
545+
}
546+
547+
// All is well, login the shop
548+
$this->auth->login($shop);
549+
}
515550
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Messaging\Events;
4+
5+
use Illuminate\Foundation\Events\Dispatchable;
6+
use Illuminate\Queue\SerializesModels;
7+
use Osiset\ShopifyApp\Objects\Values\ShopId;
8+
9+
/**
10+
* Event fired when this
11+
*/
12+
class AppInstalledEvent
13+
{
14+
use Dispatchable;
15+
use SerializesModels;
16+
17+
/**
18+
* Shop's instance.
19+
*
20+
* @var ShopId
21+
*/
22+
public $shopId;
23+
24+
/**
25+
* Create a new event instance.
26+
*
27+
* @param ShopId $shop_id
28+
*
29+
* @return void
30+
*/
31+
public function __construct(ShopId $shop_id)
32+
{
33+
$this->shopId = $shop_id;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Messaging\Events;
4+
5+
use Illuminate\Foundation\Events\Dispatchable;
6+
use Illuminate\Queue\SerializesModels;
7+
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;
8+
9+
/**
10+
* Event fired when this
11+
*/
12+
class AppUninstalledEvent
13+
{
14+
use Dispatchable;
15+
use SerializesModels;
16+
17+
/**
18+
* Shop's instance.
19+
*
20+
* @var IShopModel
21+
*/
22+
public $shop;
23+
24+
/**
25+
* Create a new event instance.
26+
*
27+
* @param IShopModel $shop
28+
*
29+
* @return void
30+
*/
31+
public function __construct(IShopModel $shop)
32+
{
33+
$this->shop = $shop;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Messaging\Events;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;
9+
use Osiset\ShopifyApp\Objects\Values\ChargeId;
10+
11+
/**
12+
* Event fired when this
13+
*/
14+
class PlanActivatedEvent
15+
{
16+
use Dispatchable;
17+
use SerializesModels;
18+
19+
/**
20+
* Shop's instance.
21+
*
22+
* @var IShopModel
23+
*/
24+
public $shop;
25+
26+
/**
27+
* Plan's instance.
28+
*
29+
* @var Model
30+
*/
31+
public $plan;
32+
33+
/**
34+
* Charge ID
35+
*
36+
* @var ChargeId
37+
*/
38+
public $chargeId;
39+
40+
/**
41+
* Create a new event instance.
42+
*
43+
* @param IShopModel $shop
44+
*
45+
* @return void
46+
*/
47+
public function __construct(IShopModel $shop, Model $plan, ChargeId $chargeId)
48+
{
49+
$this->shop = $shop;
50+
$this->plan = $plan;
51+
$this->chargeId = $chargeId;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Messaging\Events;
4+
5+
use Illuminate\Foundation\Events\Dispatchable;
6+
use Illuminate\Queue\SerializesModels;
7+
use Osiset\ShopifyApp\Objects\Values\ShopId;
8+
9+
/**
10+
* Event fired when this
11+
*/
12+
class ShopAuthenticatedEvent
13+
{
14+
use Dispatchable;
15+
use SerializesModels;
16+
17+
/**
18+
* Shop's instance.
19+
*
20+
* @var ShopId
21+
*/
22+
public $shopId;
23+
24+
/**
25+
* Create a new event instance.
26+
*
27+
* @param ShopId $shop_id
28+
*
29+
* @return void
30+
*/
31+
public function __construct(ShopId $shopId)
32+
{
33+
$this->shopId = $shopId;
34+
}
35+
}

0 commit comments

Comments
 (0)