Skip to content

Commit

Permalink
CS fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
emodric committed Sep 30, 2024
1 parent ff3d23d commit aa6af7a
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions bundle/Menu/Factory/LocationFactory/ShortcutExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Netgen\Bundle\SiteBundle\Menu\Factory\LocationFactory;

use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Core\MVC\Symfony\SiteAccess\URILexer;
use Knp\Menu\ItemInterface;
use Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value as EnhancedLinkValue;
use Netgen\IbexaSiteApi\API\LoadService;
use Netgen\IbexaSiteApi\API\Values\Content;
use Netgen\IbexaSiteApi\API\Values\Location;
Expand Down Expand Up @@ -38,10 +39,11 @@ public function matches(Location $location): bool

public function buildItem(ItemInterface $item, Location $location): void
{
/** @var EnhancedLinkValue $link */
/** @var \Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value $link */
$link = $location->content->getField('link')->value;

$this->buildItemFromContent($item, $location->content);

if ($link->isTargetLinkInNewTab()) {
$item->setLinkAttribute('target', '_blank')
->setLinkAttribute('rel', 'nofollow noopener noreferrer');
Expand All @@ -50,18 +52,22 @@ public function buildItem(ItemInterface $item, Location $location): void

private function buildItemFromContent(ItemInterface $item, Content $content): void
{
/** @var EnhancedLinkValue $link */
/** @var \Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value $link */
$link = $content->getField('link')->value;

if ($link->isTypeExternal() && is_string($link->reference)) {
$this->buildItemFromUrl($item, $link, $content);
$this->buildItemFromUrl($item, $content);

return;
}

$relatedContent = null;
if ($link->isTypeInternal() && is_int($link->reference) && $link->reference > 0) {
$relatedContent = $this->loadService->loadContent($link->reference);
try {
$relatedContent = $this->loadService->loadContent($link->reference);
} catch (NotFoundException|UnauthorizedException) {
// Do nothing
}
}

if (!$relatedContent instanceof Content || !$relatedContent->mainLocation instanceof Location) {
Expand All @@ -74,12 +80,16 @@ private function buildItemFromContent(ItemInterface $item, Content $content): vo
return;
}

$this->buildItemFromRelatedContent($item, $link, $content, $relatedContent);
$this->buildItemFromRelatedContent($item, $content, $relatedContent);
}

private function buildItemFromUrl(ItemInterface $item, EnhancedLinkValue $link, Content $content): void
private function buildItemFromUrl(ItemInterface $item, Content $content): void
{
/** @var \Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value $link */
$link = $content->getField('link')->value;

$uri = $link->reference ?? '';

if (!str_starts_with($uri, 'http')) {
$request = $this->requestStack->getMainRequest();
if (!$request instanceof Request) {
Expand All @@ -103,8 +113,11 @@ private function buildItemFromUrl(ItemInterface $item, EnhancedLinkValue $link,
}
}

private function buildItemFromRelatedContent(ItemInterface $item, EnhancedLinkValue $link, Content $content, Content $relatedContent): void
private function buildItemFromRelatedContent(ItemInterface $item, Content $content, Content $relatedContent): void
{
/** @var \Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value $link */
$link = $content->getField('link')->value;

if (!$content->getField('use_shortcut_name')->value->bool) {
$item->setLabel($link->label ?? $relatedContent->name);
}
Expand Down

0 comments on commit aa6af7a

Please sign in to comment.