Skip to content

Commit

Permalink
Feature/verify theme support (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvibit authored Jun 14, 2023
1 parent c438caa commit 7f0b1f7
Show file tree
Hide file tree
Showing 34 changed files with 1,465 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/Actions/AuthenticateShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use Osiset\ShopifyApp\Contracts\ApiHelper as IApiHelper;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Util;

/**
* Authenticates a shop and fires post authentication actions.
Expand Down Expand Up @@ -49,11 +50,11 @@ class AuthenticateShop
/**
* Setup.
*
* @param IApiHelper $apiHelper The API helper.
* @param InstallShop $installShopAction The action for installing a shop.
* @param DispatchScripts $dispatchScriptsAction The action for dispatching scripts.
* @param DispatchWebhooks $dispatchWebhooksAction The action for dispatching webhooks.
* @param AfterAuthorize $afterAuthorizeAction The action for after authorize actions.
* @param IApiHelper $apiHelper The API helper.
* @param InstallShop $installShopAction The action for installing a shop.
* @param DispatchScripts $dispatchScriptsAction The action for dispatching scripts.
* @param DispatchWebhooks $dispatchWebhooksAction The action for dispatching webhooks.
* @param AfterAuthorize $afterAuthorizeAction The action for after authorize actions.
*
* @return void
*/
Expand Down Expand Up @@ -101,7 +102,10 @@ public function __invoke(Request $request): array
}

// Fire the post processing jobs
call_user_func($this->dispatchScriptsAction, $result['shop_id'], false);
if (in_array($result['theme_support_level'], Util::getShopifyConfig('theme_support.unacceptable_levels'))) {
call_user_func($this->dispatchScriptsAction, $result['shop_id'], false);
}

call_user_func($this->dispatchWebhooksAction, $result['shop_id'], false);
call_user_func($this->afterAuthorizeAction, $result['shop_id']);

Expand Down
19 changes: 18 additions & 1 deletion src/Actions/InstallShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Osiset\ShopifyApp\Objects\Values\AccessToken;
use Osiset\ShopifyApp\Objects\Values\NullAccessToken;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Objects\Values\ThemeSupportLevel;
use Osiset\ShopifyApp\Util;

/**
Expand All @@ -30,19 +31,29 @@ class InstallShop
*/
protected $shopCommand;

/**
* The action for verify theme support
*
* @var VerifyThemeSupport
*/
protected $verifyThemeSupport;

/**
* Setup.
*
* @param IShopQuery $shopQuery The querier for the shop.
* @param VerifyThemeSupport $verifyThemeSupport The action for verify theme support
*
* @return void
*/
public function __construct(
IShopQuery $shopQuery,
IShopCommand $shopCommand
IShopCommand $shopCommand,
VerifyThemeSupport $verifyThemeSupport
) {
$this->shopQuery = $shopQuery;
$this->shopCommand = $shopCommand;
$this->verifyThemeSupport = $verifyThemeSupport;
}

/**
Expand All @@ -57,6 +68,7 @@ public function __invoke(ShopDomain $shopDomain, ?string $code): array
{
// Get the shop
$shop = $this->shopQuery->getByDomain($shopDomain, [], true);

if ($shop === null) {
// Shop does not exist, make them and re-get
$this->shopCommand->make($shopDomain, NullAccessToken::fromNative(null));
Expand Down Expand Up @@ -88,17 +100,22 @@ public function __invoke(ShopDomain $shopDomain, ?string $code): array
$data = $apiHelper->getAccessData($code);
$this->shopCommand->setAccessToken($shop->getId(), AccessToken::fromNative($data['access_token']));

$themeSupportLevel = call_user_func($this->verifyThemeSupport, $shop->getId());
$this->shopCommand->setThemeSupportLevel($shop->getId(), ThemeSupportLevel::fromNative($themeSupportLevel));

return [
'completed' => true,
'url' => null,
'shop_id' => $shop->getId(),
'theme_support_level' => $themeSupportLevel,
];
} catch (Exception $e) {
// Just return the default setting
return [
'completed' => false,
'url' => null,
'shop_id' => null,
'theme_support_level' => null,
];
}
}
Expand Down
79 changes: 79 additions & 0 deletions src/Actions/VerifyThemeSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Osiset\ShopifyApp\Actions;

use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
use Osiset\ShopifyApp\Objects\Enums\ThemeSupportLevel;
use Osiset\ShopifyApp\Objects\Values\ShopId;
use Osiset\ShopifyApp\Services\ThemeHelper;

/**
* Activates a plan for a shop.
*/
class VerifyThemeSupport
{
/**
* Querier for shops.
*
* @var IShopQuery
*/
protected $shopQuery;

/**
* Theme helper.
*
* @var ThemeHelper
*/
protected $themeHelper;

/**
* Setup.
*
* @param IShopQuery $shopQuery The querier for shops.
* @param ThemeHelper $themeHelper Theme helper.
*
* @return void
*/
public function __construct(
IShopQuery $shopQuery,
ThemeHelper $themeHelper
) {
$this->shopQuery = $shopQuery;
$this->themeHelper = $themeHelper;
}

/**
* Execution.
*
* @param ShopId $shopId The shop ID.
*
* @return int
*/
public function __invoke(ShopId $shopId): int
{
$this->themeHelper->extractStoreMainTheme($shopId);

if ($this->themeHelper->themeIsReady()) {
$templateJSONFiles = $this->themeHelper->templateJSONFiles();
$templateMainSections = $this->themeHelper->mainSections($templateJSONFiles);
$sectionsWithAppBlock = $this->themeHelper->sectionsWithAppBlock($templateMainSections);

$hasTemplates = count($templateJSONFiles) > 0;
$allTemplatesHasRightType = count($templateJSONFiles) === count($sectionsWithAppBlock);
$templatesСountWithRightType = count($sectionsWithAppBlock);

switch (true) {
case $hasTemplates && $allTemplatesHasRightType:
return ThemeSupportLevel::FULL;

case $templatesСountWithRightType:
return ThemeSupportLevel::PARTIAL;

default:
return ThemeSupportLevel::UNSUPPORTED;
}
}

return ThemeSupportLevel::UNSUPPORTED;
}
}
11 changes: 11 additions & 0 deletions src/Contracts/Commands/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Osiset\ShopifyApp\Contracts\Objects\Values\PlanId as PlanIdValue;
use Osiset\ShopifyApp\Contracts\Objects\Values\ShopDomain as ShopDomainValue;
use Osiset\ShopifyApp\Contracts\Objects\Values\ShopId as ShopIdValue;
use Osiset\ShopifyApp\Contracts\Objects\Values\ThemeSupportLevel as ThemeSupportLevelValue;

/**
* Represents commands for shops.
Expand Down Expand Up @@ -42,6 +43,16 @@ public function setToPlan(ShopIdValue $shopId, PlanIdValue $planId): bool;
*/
public function setAccessToken(ShopIdValue $shopId, AccessTokenValue $token): bool;

/**
* Sets the Online Store 2.0 support level
*
* @param ShopIdValue $shopId The shop's ID.
* @param ThemeSupportLevel $themeSupportLevel Support level
*
* @return bool
*/
public function setThemeSupportLevel(ShopIdValue $shopId, ThemeSupportLevelValue $themeSupportLevel): bool;

/**
* Cleans the shop's properties (token, plan).
* Used for uninstalls.
Expand Down
12 changes: 12 additions & 0 deletions src/Contracts/Objects/Values/ThemeId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Osiset\ShopifyApp\Contracts\Objects\Values;

use Funeralzone\ValueObjects\ValueObject;

/**
* Theme's ID value object.
*/
interface ThemeId extends ValueObject
{
}
12 changes: 12 additions & 0 deletions src/Contracts/Objects/Values/ThemeName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Osiset\ShopifyApp\Contracts\Objects\Values;

use Funeralzone\ValueObjects\ValueObject;

/**
* Theme's name value object.
*/
interface ThemeName extends ValueObject
{
}
12 changes: 12 additions & 0 deletions src/Contracts/Objects/Values/ThemeRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Osiset\ShopifyApp\Contracts\Objects\Values;

use Funeralzone\ValueObjects\ValueObject;

/**
* Theme's role value object.
*/
interface ThemeRole extends ValueObject
{
}
12 changes: 12 additions & 0 deletions src/Contracts/Objects/Values/ThemeSupportLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Osiset\ShopifyApp\Contracts\Objects\Values;

use Funeralzone\ValueObjects\ValueObject;

/**
* Access token value object.
*/
interface ThemeSupportLevel extends ValueObject
{
}
35 changes: 35 additions & 0 deletions src/Objects/Enums/ThemeSupportLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Osiset\ShopifyApp\Objects\Enums;

use Funeralzone\ValueObjects\Enums\EnumTrait;
use Funeralzone\ValueObjects\ValueObject;

/**
* Online Store 2.0 theme support
*/
class ThemeSupportLevel implements ValueObject
{
use EnumTrait;

/**
* Support level: fully.
*
* @var int
*/
public const FULL = 0;

/**
* Support level: partial.
*
* @var int
*/
public const PARTIAL = 1;

/**
* Support level: unsupported.
*
* @var int
*/
public const UNSUPPORTED = 2;
}
96 changes: 96 additions & 0 deletions src/Objects/Values/MainTheme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Osiset\ShopifyApp\Objects\Values;

use Funeralzone\ValueObjects\CompositeTrait;
use Funeralzone\ValueObjects\ValueObject;
use Illuminate\Support\Arr;
use Osiset\ShopifyApp\Contracts\Objects\Values\ThemeId as ThemeIdValue;
use Osiset\ShopifyApp\Contracts\Objects\Values\ThemeName as ThemeNameValue;
use Osiset\ShopifyApp\Contracts\Objects\Values\ThemeRole as ThemeRoleValue;

/**
* Used to inject current session data into the user's model.
* TODO: Possibly move this to a composite VO?
*/
final class MainTheme implements ValueObject
{
use CompositeTrait;

/**
* Theme id
*
* @var ThemeId
*/
protected $id;

/**
* Theme name
*
* @var ThemeName
*/
protected $name;

/**
* Theme role
*
* @var ThemeRole
*/
protected $role;

/**
* __construct
*
* @param ThemeIdValue $id
* @param ThemeNameValue $name
* @param ThemeRoleValue $role
*/
public function __construct(ThemeIdValue $id, ThemeNameValue $name, ThemeRoleValue $role)
{
$this->id = $id;
$this->name = $name;
$this->role = $role;
}

/**
* {@inheritDoc}
*/
public static function fromNative($native)
{
return new static(
NullableThemeId::fromNative(Arr::get($native, 'id')),
NullableThemeName::fromNative(Arr::get($native, 'name')),
NullableThemeRole::fromNative(Arr::get($native, 'role'))
);
}

/**
* Get theme id
*
* @return ThemeId
*/
public function getId()
{
return $this->id;
}

/**
* Get theme name
*
* @return ThemeName
*/
public function getName()
{
return $this->name;
}

/**
* Get theme role
*
* @return ThemeRole
*/
public function getRole()
{
return $this->role;
}
}
Loading

0 comments on commit 7f0b1f7

Please sign in to comment.