Skip to content

Commit

Permalink
Implemented the Exceptions as one liners
Browse files Browse the repository at this point in the history
only triggered when in development mode and debug is enabled otherwise
it will just be a debug log record
  • Loading branch information
JeroenBoersma committed Jun 6, 2024
1 parent 5b2271f commit 3b4f6bb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 59 deletions.
4 changes: 4 additions & 0 deletions src/Block/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Elgentos\PrismicIO\Block;

use Elgentos\PrismicIO\Block\Exception\DocumentIsBrokenException;
use Elgentos\PrismicIO\Block\Exception\DocumentIsNotFoundException;
use Elgentos\PrismicIO\Model\Api;
use Elgentos\PrismicIO\ViewModel\DocumentResolver;
use Elgentos\PrismicIO\ViewModel\LinkResolver;
Expand Down Expand Up @@ -53,6 +55,7 @@ private function fetchChildDocument(): bool

$isBroken = (bool)($context->isBroken ?? true);
if ($isBroken) {
DocumentIsBrokenException::throwException($this);
// We can only query existing pages
return false;
}
Expand All @@ -67,6 +70,7 @@ private function fetchChildDocument(): bool
}

if (! $document) {
DocumentIsNotFoundException::throwException($this);
return false;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Block/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

namespace Elgentos\PrismicIO\Block;

use Elgentos\PrismicIO\Block\Exception\GroupExpectsAnArrayException;

class Group extends AbstractBlock
{
public function fetchDocumentView(): string
{
$items = $this->getContext();
if (! is_array($items)) {
GroupExpectsAnArrayException::throwException($this);
return '';
}

Expand Down
3 changes: 3 additions & 0 deletions src/Block/SliceBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Elgentos\PrismicIO\Block;

use Elgentos\PrismicIO\Block\Exception\SliceBlockNotFoundException;

class SliceBlock extends AbstractBlock
{
protected function _toHtml(): string
Expand All @@ -17,6 +19,7 @@ public function fetchDocumentView(): string

$block = $layout->getBlock($reference);
if (! ($block instanceof BlockInterface)) {
SliceBlockNotFoundException::throwException($this);
return '';
}

Expand Down
11 changes: 11 additions & 0 deletions src/Block/SliceVariation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

namespace Elgentos\PrismicIO\Block;

use Elgentos\PrismicIO\Block\Exception\SliceVariationNotFoundException;
use Elgentos\PrismicIO\Block\Exception\SliceVariationNoVariationException;

class SliceVariation extends AbstractBlock
{
public function fetchDocumentView(): string
{
$context = $this->getContext();
$variation = $context->variation ?: null;
if (! $variation) {
SliceVariationNoVariationException::throwException($this);
return '';
}

$block = $this->getChildBlock($variation);
if (! ($block instanceof BlockInterface)) {
SliceVariationNotFoundException::throwException(
$this,
[
'variation' => $variation,
],
);

return '';
}

Expand Down
5 changes: 5 additions & 0 deletions src/Block/Slices.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace Elgentos\PrismicIO\Block;

use Elgentos\PrismicIO\Block\Exception\SliceNotFoundException;

class Slices extends Group
{
public function fetchItem(\stdClass $slice, $key = null): string
{
$sliceTypeBlock = $this->getSliceTypeBlock($slice->slice_type);
if (null === $sliceTypeBlock) {
SliceNotFoundException::throwException($this, [
'slice_type' => $slice->slice_type,
]);
return '';
}

Expand Down
69 changes: 22 additions & 47 deletions src/Block/StaticBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,39 @@

namespace Elgentos\PrismicIO\Block;

use Elgentos\PrismicIO\Exception\ApiNotEnabledException;
use Elgentos\PrismicIO\Block\Exception\StaticBlockNotFoundException;
use Elgentos\PrismicIO\Exception\ContextNotFoundException;
use Elgentos\PrismicIO\Exception\DocumentNotFoundException;
use Elgentos\PrismicIO\Model\Api;
use Elgentos\PrismicIO\ViewModel\DocumentResolver;
use Elgentos\PrismicIO\ViewModel\LinkResolver;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Context;
use Psr\Log\LoggerInterface;
use stdClass;

class StaticBlock extends AbstractBlock
{
private Api $api;

private LoggerInterface $logger;

private string $contentType;

private ?string $identifier;

public function __construct(
Context $context,
DocumentResolver $documentResolver,
LinkResolver $linkResolver,
Api $api,
LoggerInterface $logger,
string $contentType = 'static_block',
string $identifier = null,
array $data = []
Context $context,
DocumentResolver $documentResolver,
LinkResolver $linkResolver,
private readonly Api $api,
private readonly string $contentType = 'static_block',
private readonly ?string $identifier = null,
array $data = []
) {
parent::__construct($context, $documentResolver, $linkResolver, $data);
$this->api = $api;
$this->logger = $logger;
$this->contentType = $contentType;
$this->identifier = $identifier;
}

/**
* @return string
* @throws NoSuchEntityException
*/
protected function _toHtml(): string
{
$this->createPrismicDocument();
return parent::_toHtml();
}

/**
* @return void
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function createPrismicDocument(): void
{
$contentType = $this->contentType;
$identifier = $this->identifier;


// Allow using "template" to reference a document (saves XML)
$reference = $this->getReference();
if ($reference !== '*') {
Expand All @@ -79,6 +54,7 @@ private function createPrismicDocument(): void
return;
}

// Create a document
$document = new \stdClass;
$options = $this->api->getOptions();

Expand All @@ -96,19 +72,15 @@ private function createPrismicDocument(): void
*/
public function fetchDocumentView(): string
{
try {
if (!$this->fetchChildDocument()) {
return '';
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
if (! $this->fetchChildDocument()) {
return '';
}

// Render all children
$html = '';
foreach ($this->getChildNames() as $childName) {
$useCache = ! $this->updateChildDocumentWithDocument($childName);
$html .= $this->getChildHtml($childName, $useCache);
$useCache = ! $this->updateChildDocumentWithDocument($childName);
$html .= $this->getChildHtml($childName, $useCache);
}

return $html;
Expand All @@ -129,13 +101,16 @@ private function fetchChildDocument(): bool
$uid = $context->uid ?? '';
$type = $context->type ?? '';

try {
$document = $this->api->getDocumentByUid($uid, $type, ['lang' => $context->lang]);
} catch (\Exception $e) {
return false;
}

$document = $this->api->getDocumentByUid($uid, $type, ['lang' => $context->lang]);
if (! $document) {
StaticBlockNotFoundException::throwException(
$this,
[
'uid' => $uid,
'content_type' => $type,
'language' => $context->lang,
]
);
return false;
}

Expand Down
15 changes: 3 additions & 12 deletions src/Model/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,7 @@ public function getDocumentByUid(string $uid, string $contentType = null, array
return null;
}

try {
$document = $api->getByUID($contentType, $uid, $this->getOptions($options));
} catch (Exception $e) {
return null;
}

$document = $api->getByUID($contentType, $uid, $this->getOptions($options));
if ($document || ! $this->isFallbackAllowed()) {
return $document;
}
Expand Down Expand Up @@ -292,12 +287,8 @@ public function getSingleton(string $contentType = null, array $options = []): ?
*/
public function getDocumentById(string $id, array $options = []): ?\stdClass
{
$api = $this->create();
try {
return $api->getByID($id, $this->getOptions($options));
} catch (Exception $e) {
return null;
}
return $this->create()
->getByID($id, $this->getOptions($options));
}

}

0 comments on commit 3b4f6bb

Please sign in to comment.