Skip to content

Commit 7f0b1f7

Browse files
authored
Feature/verify theme support (#69)
1 parent c438caa commit 7f0b1f7

34 files changed

+1465
-12
lines changed

src/Actions/AuthenticateShop.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Http\Request;
66
use Osiset\ShopifyApp\Contracts\ApiHelper as IApiHelper;
77
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
8+
use Osiset\ShopifyApp\Util;
89

910
/**
1011
* Authenticates a shop and fires post authentication actions.
@@ -49,11 +50,11 @@ class AuthenticateShop
4950
/**
5051
* Setup.
5152
*
52-
* @param IApiHelper $apiHelper The API helper.
53-
* @param InstallShop $installShopAction The action for installing a shop.
54-
* @param DispatchScripts $dispatchScriptsAction The action for dispatching scripts.
55-
* @param DispatchWebhooks $dispatchWebhooksAction The action for dispatching webhooks.
56-
* @param AfterAuthorize $afterAuthorizeAction The action for after authorize actions.
53+
* @param IApiHelper $apiHelper The API helper.
54+
* @param InstallShop $installShopAction The action for installing a shop.
55+
* @param DispatchScripts $dispatchScriptsAction The action for dispatching scripts.
56+
* @param DispatchWebhooks $dispatchWebhooksAction The action for dispatching webhooks.
57+
* @param AfterAuthorize $afterAuthorizeAction The action for after authorize actions.
5758
*
5859
* @return void
5960
*/
@@ -101,7 +102,10 @@ public function __invoke(Request $request): array
101102
}
102103

103104
// Fire the post processing jobs
104-
call_user_func($this->dispatchScriptsAction, $result['shop_id'], false);
105+
if (in_array($result['theme_support_level'], Util::getShopifyConfig('theme_support.unacceptable_levels'))) {
106+
call_user_func($this->dispatchScriptsAction, $result['shop_id'], false);
107+
}
108+
105109
call_user_func($this->dispatchWebhooksAction, $result['shop_id'], false);
106110
call_user_func($this->afterAuthorizeAction, $result['shop_id']);
107111

src/Actions/InstallShop.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Osiset\ShopifyApp\Objects\Values\AccessToken;
1010
use Osiset\ShopifyApp\Objects\Values\NullAccessToken;
1111
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
12+
use Osiset\ShopifyApp\Objects\Values\ThemeSupportLevel;
1213
use Osiset\ShopifyApp\Util;
1314

1415
/**
@@ -30,19 +31,29 @@ class InstallShop
3031
*/
3132
protected $shopCommand;
3233

34+
/**
35+
* The action for verify theme support
36+
*
37+
* @var VerifyThemeSupport
38+
*/
39+
protected $verifyThemeSupport;
40+
3341
/**
3442
* Setup.
3543
*
3644
* @param IShopQuery $shopQuery The querier for the shop.
45+
* @param VerifyThemeSupport $verifyThemeSupport The action for verify theme support
3746
*
3847
* @return void
3948
*/
4049
public function __construct(
4150
IShopQuery $shopQuery,
42-
IShopCommand $shopCommand
51+
IShopCommand $shopCommand,
52+
VerifyThemeSupport $verifyThemeSupport
4353
) {
4454
$this->shopQuery = $shopQuery;
4555
$this->shopCommand = $shopCommand;
56+
$this->verifyThemeSupport = $verifyThemeSupport;
4657
}
4758

4859
/**
@@ -57,6 +68,7 @@ public function __invoke(ShopDomain $shopDomain, ?string $code): array
5768
{
5869
// Get the shop
5970
$shop = $this->shopQuery->getByDomain($shopDomain, [], true);
71+
6072
if ($shop === null) {
6173
// Shop does not exist, make them and re-get
6274
$this->shopCommand->make($shopDomain, NullAccessToken::fromNative(null));
@@ -88,17 +100,22 @@ public function __invoke(ShopDomain $shopDomain, ?string $code): array
88100
$data = $apiHelper->getAccessData($code);
89101
$this->shopCommand->setAccessToken($shop->getId(), AccessToken::fromNative($data['access_token']));
90102

103+
$themeSupportLevel = call_user_func($this->verifyThemeSupport, $shop->getId());
104+
$this->shopCommand->setThemeSupportLevel($shop->getId(), ThemeSupportLevel::fromNative($themeSupportLevel));
105+
91106
return [
92107
'completed' => true,
93108
'url' => null,
94109
'shop_id' => $shop->getId(),
110+
'theme_support_level' => $themeSupportLevel,
95111
];
96112
} catch (Exception $e) {
97113
// Just return the default setting
98114
return [
99115
'completed' => false,
100116
'url' => null,
101117
'shop_id' => null,
118+
'theme_support_level' => null,
102119
];
103120
}
104121
}

src/Actions/VerifyThemeSupport.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Actions;
4+
5+
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
6+
use Osiset\ShopifyApp\Objects\Enums\ThemeSupportLevel;
7+
use Osiset\ShopifyApp\Objects\Values\ShopId;
8+
use Osiset\ShopifyApp\Services\ThemeHelper;
9+
10+
/**
11+
* Activates a plan for a shop.
12+
*/
13+
class VerifyThemeSupport
14+
{
15+
/**
16+
* Querier for shops.
17+
*
18+
* @var IShopQuery
19+
*/
20+
protected $shopQuery;
21+
22+
/**
23+
* Theme helper.
24+
*
25+
* @var ThemeHelper
26+
*/
27+
protected $themeHelper;
28+
29+
/**
30+
* Setup.
31+
*
32+
* @param IShopQuery $shopQuery The querier for shops.
33+
* @param ThemeHelper $themeHelper Theme helper.
34+
*
35+
* @return void
36+
*/
37+
public function __construct(
38+
IShopQuery $shopQuery,
39+
ThemeHelper $themeHelper
40+
) {
41+
$this->shopQuery = $shopQuery;
42+
$this->themeHelper = $themeHelper;
43+
}
44+
45+
/**
46+
* Execution.
47+
*
48+
* @param ShopId $shopId The shop ID.
49+
*
50+
* @return int
51+
*/
52+
public function __invoke(ShopId $shopId): int
53+
{
54+
$this->themeHelper->extractStoreMainTheme($shopId);
55+
56+
if ($this->themeHelper->themeIsReady()) {
57+
$templateJSONFiles = $this->themeHelper->templateJSONFiles();
58+
$templateMainSections = $this->themeHelper->mainSections($templateJSONFiles);
59+
$sectionsWithAppBlock = $this->themeHelper->sectionsWithAppBlock($templateMainSections);
60+
61+
$hasTemplates = count($templateJSONFiles) > 0;
62+
$allTemplatesHasRightType = count($templateJSONFiles) === count($sectionsWithAppBlock);
63+
$templatesСountWithRightType = count($sectionsWithAppBlock);
64+
65+
switch (true) {
66+
case $hasTemplates && $allTemplatesHasRightType:
67+
return ThemeSupportLevel::FULL;
68+
69+
case $templatesСountWithRightType:
70+
return ThemeSupportLevel::PARTIAL;
71+
72+
default:
73+
return ThemeSupportLevel::UNSUPPORTED;
74+
}
75+
}
76+
77+
return ThemeSupportLevel::UNSUPPORTED;
78+
}
79+
}

src/Contracts/Commands/Shop.php

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Osiset\ShopifyApp\Contracts\Objects\Values\PlanId as PlanIdValue;
77
use Osiset\ShopifyApp\Contracts\Objects\Values\ShopDomain as ShopDomainValue;
88
use Osiset\ShopifyApp\Contracts\Objects\Values\ShopId as ShopIdValue;
9+
use Osiset\ShopifyApp\Contracts\Objects\Values\ThemeSupportLevel as ThemeSupportLevelValue;
910

1011
/**
1112
* Represents commands for shops.
@@ -42,6 +43,16 @@ public function setToPlan(ShopIdValue $shopId, PlanIdValue $planId): bool;
4243
*/
4344
public function setAccessToken(ShopIdValue $shopId, AccessTokenValue $token): bool;
4445

46+
/**
47+
* Sets the Online Store 2.0 support level
48+
*
49+
* @param ShopIdValue $shopId The shop's ID.
50+
* @param ThemeSupportLevel $themeSupportLevel Support level
51+
*
52+
* @return bool
53+
*/
54+
public function setThemeSupportLevel(ShopIdValue $shopId, ThemeSupportLevelValue $themeSupportLevel): bool;
55+
4556
/**
4657
* Cleans the shop's properties (token, plan).
4758
* Used for uninstalls.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Contracts\Objects\Values;
4+
5+
use Funeralzone\ValueObjects\ValueObject;
6+
7+
/**
8+
* Theme's ID value object.
9+
*/
10+
interface ThemeId extends ValueObject
11+
{
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Contracts\Objects\Values;
4+
5+
use Funeralzone\ValueObjects\ValueObject;
6+
7+
/**
8+
* Theme's name value object.
9+
*/
10+
interface ThemeName extends ValueObject
11+
{
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Contracts\Objects\Values;
4+
5+
use Funeralzone\ValueObjects\ValueObject;
6+
7+
/**
8+
* Theme's role value object.
9+
*/
10+
interface ThemeRole extends ValueObject
11+
{
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Contracts\Objects\Values;
4+
5+
use Funeralzone\ValueObjects\ValueObject;
6+
7+
/**
8+
* Access token value object.
9+
*/
10+
interface ThemeSupportLevel extends ValueObject
11+
{
12+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Objects\Enums;
4+
5+
use Funeralzone\ValueObjects\Enums\EnumTrait;
6+
use Funeralzone\ValueObjects\ValueObject;
7+
8+
/**
9+
* Online Store 2.0 theme support
10+
*/
11+
class ThemeSupportLevel implements ValueObject
12+
{
13+
use EnumTrait;
14+
15+
/**
16+
* Support level: fully.
17+
*
18+
* @var int
19+
*/
20+
public const FULL = 0;
21+
22+
/**
23+
* Support level: partial.
24+
*
25+
* @var int
26+
*/
27+
public const PARTIAL = 1;
28+
29+
/**
30+
* Support level: unsupported.
31+
*
32+
* @var int
33+
*/
34+
public const UNSUPPORTED = 2;
35+
}

src/Objects/Values/MainTheme.php

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Objects\Values;
4+
5+
use Funeralzone\ValueObjects\CompositeTrait;
6+
use Funeralzone\ValueObjects\ValueObject;
7+
use Illuminate\Support\Arr;
8+
use Osiset\ShopifyApp\Contracts\Objects\Values\ThemeId as ThemeIdValue;
9+
use Osiset\ShopifyApp\Contracts\Objects\Values\ThemeName as ThemeNameValue;
10+
use Osiset\ShopifyApp\Contracts\Objects\Values\ThemeRole as ThemeRoleValue;
11+
12+
/**
13+
* Used to inject current session data into the user's model.
14+
* TODO: Possibly move this to a composite VO?
15+
*/
16+
final class MainTheme implements ValueObject
17+
{
18+
use CompositeTrait;
19+
20+
/**
21+
* Theme id
22+
*
23+
* @var ThemeId
24+
*/
25+
protected $id;
26+
27+
/**
28+
* Theme name
29+
*
30+
* @var ThemeName
31+
*/
32+
protected $name;
33+
34+
/**
35+
* Theme role
36+
*
37+
* @var ThemeRole
38+
*/
39+
protected $role;
40+
41+
/**
42+
* __construct
43+
*
44+
* @param ThemeIdValue $id
45+
* @param ThemeNameValue $name
46+
* @param ThemeRoleValue $role
47+
*/
48+
public function __construct(ThemeIdValue $id, ThemeNameValue $name, ThemeRoleValue $role)
49+
{
50+
$this->id = $id;
51+
$this->name = $name;
52+
$this->role = $role;
53+
}
54+
55+
/**
56+
* {@inheritDoc}
57+
*/
58+
public static function fromNative($native)
59+
{
60+
return new static(
61+
NullableThemeId::fromNative(Arr::get($native, 'id')),
62+
NullableThemeName::fromNative(Arr::get($native, 'name')),
63+
NullableThemeRole::fromNative(Arr::get($native, 'role'))
64+
);
65+
}
66+
67+
/**
68+
* Get theme id
69+
*
70+
* @return ThemeId
71+
*/
72+
public function getId()
73+
{
74+
return $this->id;
75+
}
76+
77+
/**
78+
* Get theme name
79+
*
80+
* @return ThemeName
81+
*/
82+
public function getName()
83+
{
84+
return $this->name;
85+
}
86+
87+
/**
88+
* Get theme role
89+
*
90+
* @return ThemeRole
91+
*/
92+
public function getRole()
93+
{
94+
return $this->role;
95+
}
96+
}

0 commit comments

Comments
 (0)