Skip to content

Commit

Permalink
NGSTACK-842 inject content and location handlers in contentlocation h…
Browse files Browse the repository at this point in the history
…andler
  • Loading branch information
petarjakopec committed Jun 25, 2024
1 parent e155d4b commit 2749908
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 49 deletions.
5 changes: 2 additions & 3 deletions bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ services:

Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\ContentAndLocation:
arguments:
- '@ibexa.api.repository'
- '@ibexa.api.service.content'
- '@ibexa.api.service.location'
- '@Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\Content'
- '@Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\Location'
tags:
- { name: netgen.ibexa_scheduled_visibility.handler, identifier: content_location }

Expand Down
54 changes: 10 additions & 44 deletions bundle/ScheduledVisibility/Handler/ContentAndLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,37 @@

namespace Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler;

use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Core\Repository\Repository;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\Content as ContentHandler;
use Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\Location as LocationHandler;
use Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\ScheduledVisibilityInterface;

final class ContentAndLocation implements ScheduledVisibilityInterface
{
public function __construct(
private readonly Repository $repository,
private readonly ContentService $contentService,
private readonly LocationService $locationService,
private readonly ContentHandler $contentHandler,
private readonly LocationHandler $locationHandler,
) {}

public function hide(Content $content): void
{
$this->repository->sudo(
function () use ($content): void {
$this->contentService->hideContent($content->getContentInfo());

$locations = $this->locationService->loadLocations($content->getContentInfo());

foreach ($locations as $location) {
$this->locationService->hideLocation($location);
}
},
);
$this->contentHandler->hide($content);
$this->locationHandler->hide($content);
}

public function reveal(Content $content): void
{
$this->repository->sudo(
function () use ($content): void {
$this->contentService->revealContent($content->getContentInfo());

$locations = $this->locationService->loadLocations($content->getContentInfo());

foreach ($locations as $location) {
$this->locationService->unhideLocation($location);
}
},
);
$this->contentHandler->reveal($content);
$this->locationHandler->reveal($content);
}

public function isHidden(Content $content): bool
{
return !$this->isVisible($content);
return $this->contentHandler->isHidden($content) && $this->locationHandler->isHidden($content);
}

public function isVisible(Content $content): bool
{
if ($content->contentInfo->isHidden()) {
return false;
}

$locations = $this->repository->sudo(
fn () => $this->locationService->loadLocations($content->getContentInfo()),
);

foreach ($locations as $location) {
if ($location->isHidden()) {
return false;
}
}

return true;
return $this->contentHandler->isVisible($content) && $this->locationHandler->isVisible($content);
}
}
10 changes: 8 additions & 2 deletions tests/bundle/Integration/ContentAndLocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Netgen\IbexaScheduledVisibility\Tests\Integration;

use Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\Content;
use Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\ContentAndLocation;
use Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\Location;

final class ContentAndLocationTest extends BaseTest
{
Expand All @@ -28,8 +30,12 @@ public function testUpdateVisibility(array $configuration, bool $expectedHidden)

private function getContentAndLocationHandler(): ContentAndLocation
{
$repository = $this->getRepository();
/** @var \Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\Content $contentHandler */
$contentHandler = $this->getSetupFactory()->getServiceContainer()->get(Content::class);

return new ContentAndLocation($repository, $repository->getContentService(), $repository->getLocationService());
/** @var \Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Handler\Location $locationHandler */
$locationHandler = $this->getSetupFactory()->getServiceContainer()->get(Location::class);

return new ContentAndLocation($contentHandler, $locationHandler);
}
}

0 comments on commit 2749908

Please sign in to comment.