This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
AuthShopHandler.php
197 lines (175 loc) · 4.68 KB
/
AuthShopHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace OhMyBrew\ShopifyApp\Services;
use stdClass;
use Exception;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\URL;
use OhMyBrew\ShopifyApp\Facades\ShopifyApp;
use OhMyBrew\ShopifyApp\Jobs\ScripttagInstaller;
use OhMyBrew\ShopifyApp\Jobs\WebhookInstaller;
use OhMyBrew\ShopifyApp\Models\Shop;
/**
* Responsible for handling how to authenticate a shop.
*/
class AuthShopHandler
{
/**
* The shop.
*
* @var \OhMyBrew\ShopifyApp\Models\Shop
*/
protected $shop;
/**
* The shop API.
*
* @var \OhMyBrew\BasicShopifyAPI
*/
protected $api;
/**
* Constructor for auth shop handler.
*
* @param \OhMyBrew\ShopifyApp\Models\Shop $shop The shop.
*
* @return self
*/
public function __construct(shop $shop)
{
// Setup the API
$this->shop = $shop;
$this->api = ShopifyApp::api();
$this->api->setShop($this->shop->shopify_domain);
return $this;
}
/**
* Builds the authentication URL for a shop.
*
* @param string|null $mode The mode of grant ("offline"/"per-user").
*
* @return string
*/
public function buildAuthUrl($mode = null)
{
// Determine the type of mode
// Grab the authentication URL
return $this->api->getAuthUrl(
Config::get('shopify-app.api_scopes'),
URL::secure(Config::get('shopify-app.api_redirect')),
$mode ?? 'offline'
);
}
/**
* Determines if the request HMAC is verified.
*
* @param array $request The request parameters.
*
* @return bool
*/
public function verifyRequest(array $request)
{
return $this->api->verifyRequest($request);
}
/**
* Finish the process by getting the access details from the code.
*
* @param string $code The code from the request.
*
* @return stdClass
*/
public function getAccess(string $code)
{
return $this->api->requestAccess($code);
}
/**
* Post process actions after authentication is done.
*
* @return void
*/
public function postProcess()
{
if (!$this->shop->trashed()) {
return;
}
// Trashed, fix it
$this->shop->restore();
$this->shop->charges()->restore();
$this->shop->save();
}
/**
* Dispatches the jobs that happen after authentication.
*
* @return bool
*/
public function dispatchJobs()
{
$this->dispatchWebhooks();
$this->dispatchScripttags();
$this->dispatchAfterAuthenticate();
return true;
}
/**
* Dispatches the job to install webhooks.
*
* @return void
*/
public function dispatchWebhooks()
{
$webhooks = Config::get('shopify-app.webhooks');
if (count($webhooks) > 0) {
WebhookInstaller::dispatch($this->shop)->onQueue(Config::get('shopify-app.job_queues.webhooks'));
}
}
/**
* Dispatches the job to install scripttags.
*
* @return void
*/
public function dispatchScripttags()
{
$scripttags = Config::get('shopify-app.scripttags');
if (count($scripttags) > 0) {
ScripttagInstaller::dispatch($this->shop, $scripttags)->onQueue(Config::get('shopify-app.job_queues.scripttags'));
}
}
/**
* Dispatches the after authenticate job, if any.
*
* @return void
*/
public function dispatchAfterAuthenticate()
{
// Grab the jobs config
$jobsConfig = Config::get('shopify-app.after_authenticate_job');
/**
* Fires the job.
*
* @param array $config The job's configuration
*
* @return bool
*/
$fireJob = function ($config) {
$job = $config['job'];
if (isset($config['inline']) && $config['inline'] === true) {
// Run this job immediately
$job::dispatchNow($this->shop);
} else {
// Run later
$job::dispatch($this->shop)->onQueue(Config::get('shopify-app.job_queues.after_authenticate'));
}
return true;
};
// We have multi-jobs
if (isset($jobsConfig[0])) {
foreach ($jobsConfig as $jobConfig) {
// We have a job, pass the shop object to the contructor
$fireJob($jobConfig);
}
return true;
}
// We have a single job
if (isset($jobsConfig['job'])) {
return $fireJob($jobsConfig);
}
return false;
}
}