This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathAfterAuthorize.php
88 lines (78 loc) · 2.22 KB
/
AfterAuthorize.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
<?php
namespace Osiset\ShopifyApp\Actions;
use Illuminate\Support\Arr;
use Osiset\ShopifyApp\Contracts\Objects\Values\ShopId as ShopIdValue;
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;
use Osiset\ShopifyApp\Util;
/**
* Run after authentication jobs.
*/
class AfterAuthorize
{
/**
* Querier for shops.
*
* @var IShopQuery
*/
protected $shopQuery;
/**
* Setup.
*
* @param IShopQuery $shopQuery The querier for the shop.
*
* @return void
*/
public function __construct(IShopQuery $shopQuery)
{
$this->shopQuery = $shopQuery;
}
/**
* Execution.
* TODO: Rethrow an API exception.
*
* @param ShopIdValue $shopId The shop ID.
*
* @return bool
*/
public function __invoke(ShopIdValue $shopId): bool
{
/**
* Fires the job.
*
* @param array $config The job's configuration.
* @param IShopModel $shop The shop instance.
*
* @return bool
*/
$fireJob = function (array $config, IShopModel $shop): bool {
$job = Arr::get($config, 'job');
if (Arr::get($config, 'inline', false)) {
// Run this job immediately
$job::dispatchSync($shop);
} else {
// Run later
$job::dispatch($shop)
->onQueue(Util::getShopifyConfig('job_queues')['after_authenticate']);
}
return true;
};
// Get the shop
$shop = $this->shopQuery->getById($shopId);
// Grab the jobs config
$jobsConfig = Util::getShopifyConfig('after_authenticate_job');
if (Arr::has($jobsConfig, 0)) {
// We have multi-jobs
foreach ($jobsConfig as $jobConfig) {
// We have a job, pass the shop object to the constructor
$fireJob($jobConfig, $shop);
}
return true;
} elseif (Arr::has($jobsConfig, 'job')) {
// We have a single job
return $fireJob($jobsConfig, $shop);
}
// We have no jobs
return false;
}
}