Skip to content

Commit

Permalink
adding better errors for store trait
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic committed Apr 30, 2024
1 parent 88e5dfe commit a0e0497
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
20 changes: 20 additions & 0 deletions src/Exception/InvalidBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,24 @@ public static function wrongComponentNameException(string $name, string $compone
)
);
}

/**
* Throws error if missing item.
*
* @param string $name Block/component name.
* @param string $type Type of the item.
*
* @return static
*/
public static function missingItemException(string $name, string $type): InvalidBlock
{
return new InvalidBlock(
\sprintf(
/* translators: %1$s is going to be replaced with the component/block name, %2$s with type. */
\esc_html__('You are trying to get %1$s %2$s. Please check if you have it in your project.', 'eightshift-libs'),
$name,
$type
)
);
}
}
4 changes: 2 additions & 2 deletions src/Exception/InvalidManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function emptyOrErrorManifestException(string $path): InvalidManif
\sprintf(
/* translators: %s is replaced by the missing key in the manifest.json */
\esc_html__(
'Manifest.json in this %s is empty or has errors. Please check your data.',
'Manifest.json in %s path is empty or has errors. Please check your data.',
'eightshift-libs'
),
$path
Expand All @@ -74,7 +74,7 @@ public static function missingManifestException(string $path): InvalidManifest
\sprintf(
/* translators: %s is replaced by the missing key in the manifest.json */
\esc_html__(
'Manifest.json in missing in this %s path. Please check your data.',
'Manifest.json in missing in %s path. Please check your data.',
'eightshift-libs'
),
$path
Expand Down
43 changes: 32 additions & 11 deletions src/Helpers/StoreBlocksTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace EightshiftLibs\Helpers;

use EightshiftLibs\Cache\AbstractManifestCache;
use EightshiftLibs\Exception\InvalidBlock;

/**
* Class StoreTrait Helper
Expand Down Expand Up @@ -99,23 +100,27 @@ public static function setBlocks(array $blocks): void
/**
* Get blocks details.
*
* @throws InvalidBlock If blocks are missing.
*
* @return array<mixed>
*/
public static function getBlocks(): array
{
return self::getStore()[AbstractManifestCache::BLOCKS_KEY] ?? [];
return self::getStore()[AbstractManifestCache::BLOCKS_KEY] ?? throw InvalidBlock::missingItemException('project', 'blocks');
}

/**
* Get block details.
*
* @throws InvalidBlock If block is missing.
*
* @param string $block Block name to get.
*
* @return array<mixed>
*/
public static function getBlock(string $block): array
{
return self::getBlocks()[$block] ?? [];
return self::getBlocks()[$block] ?? throw InvalidBlock::missingItemException($block, 'block');
}

/**
Expand All @@ -137,23 +142,27 @@ public static function setComponents(array $components): void
/**
* Get components details.
*
* @throws InvalidBlock If components are missing.
*
* @return array<mixed>
*/
public static function getComponents(): array
{
return self::getStore()[AbstractManifestCache::COMPONENTS_KEY] ?? [];
return self::getStore()[AbstractManifestCache::COMPONENTS_KEY] ?? throw InvalidBlock::missingItemException('blocks', 'components');
}

/**
* Get component details.
*
* @param string $component Componennt name to get.
*
* @throws InvalidBlock If component is missing.
*
* @return array<mixed>
*/
public static function getComponent(string $component): array
{
return self::getComponents()[$component] ?? [];
return self::getComponents()[$component] ?? throw InvalidBlock::missingItemException($component, 'component');
}

/**
Expand All @@ -175,23 +184,27 @@ public static function setVariations(array $variations): void
/**
* Get variations details.
*
* @throws InvalidBlock If variations are missing.
*
* @return array<mixed>
*/
public static function getVariations(): array
{
return self::getStore()[AbstractManifestCache::VARIATIONS_KEY] ?? [];
return self::getStore()[AbstractManifestCache::VARIATIONS_KEY] ?? throw InvalidBlock::missingItemException('blocks', 'variations');
}

/**
* Get variation details.
*
* @param string $variation Variation name to get.
*
* @throws InvalidBlock If variation is missing.
*
* @return array<mixed>
*/
public static function getVariation(string $variation): array
{
return self::getVariations()[$variation] ?? [];
return self::getVariations()[$variation] ?? throw InvalidBlock::missingItemException($variation, 'variation');
}

/**
Expand Down Expand Up @@ -483,11 +496,13 @@ public static function setWrapper(array $wrapper): void
/**
* Get wrapper details.
*
* @throws InvalidBlock If wrapper is missing.
*
* @return array<mixed>
*/
public static function getWrapper(): array
{
return self::getStore()[AbstractManifestCache::WRAPPER_KEY] ?? [];
return self::getStore()[AbstractManifestCache::WRAPPER_KEY] ?? throw InvalidBlock::missingItemException('blocks wrapper', 'component');
}

/**
Expand Down Expand Up @@ -519,11 +534,13 @@ public static function setSettings(array $settings): void
/**
* Get global settings details.
*
* @throws InvalidBlock If settings are missing.
*
* @return array<mixed>
*/
public static function getSettings(): array
{
return self::getStore()[AbstractManifestCache::SETTINGS_KEY] ?? [];
return self::getStore()[AbstractManifestCache::SETTINGS_KEY] ?? throw InvalidBlock::missingItemException('block', 'global settings');
}

/**
Expand Down Expand Up @@ -666,29 +683,33 @@ public static function setAssets(array $assets): void
global $esBlocks;

if (self::getStore()) {
$esBlocks[self::getStoreName()][AbstractManifestCache::ASSETS_KEY][] = $assets;
$esBlocks[self::getStoreName()][AbstractManifestCache::ASSETS_KEY] = $assets;
}
}

/**
* Get assets details.
*
* @throws InvalidBlock If assets are missing.
*
* @return array<mixed>
*/
public static function getAssets(): array
{
return self::getStore()[AbstractManifestCache::ASSETS_KEY] ?? [];
return self::getStore()[AbstractManifestCache::ASSETS_KEY] ?? throw InvalidBlock::missingItemException('public', 'assets');
}

/**
* Get asset details.
*
* @param string $asset Asset name to get.
*
* @throws InvalidBlock If asset is missing.
*
* @return string
*/
public static function getAsset(string $asset): string
{
return self::getAssets()[$asset] ?? '';
return self::getAssets()[$asset] ?? throw InvalidBlock::missingItemException($asset, 'public asset');
}
}

0 comments on commit a0e0497

Please sign in to comment.