diff --git a/src/Exception/InvalidBlock.php b/src/Exception/InvalidBlock.php index 07d2e0726..734606dc1 100644 --- a/src/Exception/InvalidBlock.php +++ b/src/Exception/InvalidBlock.php @@ -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 + ) + ); + } } diff --git a/src/Exception/InvalidManifest.php b/src/Exception/InvalidManifest.php index ac49287b5..c95e5a9be 100644 --- a/src/Exception/InvalidManifest.php +++ b/src/Exception/InvalidManifest.php @@ -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 @@ -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 diff --git a/src/Helpers/StoreBlocksTrait.php b/src/Helpers/StoreBlocksTrait.php index c7e3934d1..812ab44ea 100644 --- a/src/Helpers/StoreBlocksTrait.php +++ b/src/Helpers/StoreBlocksTrait.php @@ -11,6 +11,7 @@ namespace EightshiftLibs\Helpers; use EightshiftLibs\Cache\AbstractManifestCache; +use EightshiftLibs\Exception\InvalidBlock; /** * Class StoreTrait Helper @@ -99,23 +100,27 @@ public static function setBlocks(array $blocks): void /** * Get blocks details. * + * @throws InvalidBlock If blocks are missing. + * * @return array */ 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 */ public static function getBlock(string $block): array { - return self::getBlocks()[$block] ?? []; + return self::getBlocks()[$block] ?? throw InvalidBlock::missingItemException($block, 'block'); } /** @@ -137,11 +142,13 @@ public static function setComponents(array $components): void /** * Get components details. * + * @throws InvalidBlock If components are missing. + * * @return array */ public static function getComponents(): array { - return self::getStore()[AbstractManifestCache::COMPONENTS_KEY] ?? []; + return self::getStore()[AbstractManifestCache::COMPONENTS_KEY] ?? throw InvalidBlock::missingItemException('blocks', 'components'); } /** @@ -149,11 +156,13 @@ public static function getComponents(): array * * @param string $component Componennt name to get. * + * @throws InvalidBlock If component is missing. + * * @return array */ public static function getComponent(string $component): array { - return self::getComponents()[$component] ?? []; + return self::getComponents()[$component] ?? throw InvalidBlock::missingItemException($component, 'component'); } /** @@ -175,11 +184,13 @@ public static function setVariations(array $variations): void /** * Get variations details. * + * @throws InvalidBlock If variations are missing. + * * @return array */ public static function getVariations(): array { - return self::getStore()[AbstractManifestCache::VARIATIONS_KEY] ?? []; + return self::getStore()[AbstractManifestCache::VARIATIONS_KEY] ?? throw InvalidBlock::missingItemException('blocks', 'variations'); } /** @@ -187,11 +198,13 @@ public static function getVariations(): array * * @param string $variation Variation name to get. * + * @throws InvalidBlock If variation is missing. + * * @return array */ public static function getVariation(string $variation): array { - return self::getVariations()[$variation] ?? []; + return self::getVariations()[$variation] ?? throw InvalidBlock::missingItemException($variation, 'variation'); } /** @@ -483,11 +496,13 @@ public static function setWrapper(array $wrapper): void /** * Get wrapper details. * + * @throws InvalidBlock If wrapper is missing. + * * @return array */ public static function getWrapper(): array { - return self::getStore()[AbstractManifestCache::WRAPPER_KEY] ?? []; + return self::getStore()[AbstractManifestCache::WRAPPER_KEY] ?? throw InvalidBlock::missingItemException('blocks wrapper', 'component'); } /** @@ -519,11 +534,13 @@ public static function setSettings(array $settings): void /** * Get global settings details. * + * @throws InvalidBlock If settings are missing. + * * @return array */ public static function getSettings(): array { - return self::getStore()[AbstractManifestCache::SETTINGS_KEY] ?? []; + return self::getStore()[AbstractManifestCache::SETTINGS_KEY] ?? throw InvalidBlock::missingItemException('block', 'global settings'); } /** @@ -666,18 +683,20 @@ 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 */ public static function getAssets(): array { - return self::getStore()[AbstractManifestCache::ASSETS_KEY] ?? []; + return self::getStore()[AbstractManifestCache::ASSETS_KEY] ?? throw InvalidBlock::missingItemException('public', 'assets'); } /** @@ -685,10 +704,12 @@ public static function getAssets(): array * * @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'); } }