Skip to content

Commit 45c8957

Browse files
committed
NGSTACK-842 extract configuration service
1 parent 1736091 commit 45c8957

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\Bundle\IbexaScheduledVisibilityBundle\Configuration;
6+
7+
use Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Registry;
8+
use Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\ScheduledVisibilityInterface;
9+
10+
final class ScheduledVisibilityConfiguration
11+
{
12+
public function __construct(
13+
private readonly Registry $registry,
14+
private readonly string $type,
15+
private readonly bool $enabled,
16+
private readonly bool $allContentTypes,
17+
private readonly array $allowedContentTypes,
18+
) {}
19+
20+
public function getHandler(): ScheduledVisibilityInterface
21+
{
22+
return $this->registry->get($this->type);
23+
}
24+
25+
public function isEnabled(): bool
26+
{
27+
return $this->enabled;
28+
}
29+
30+
public function isAllContentTypes(): bool
31+
{
32+
return $this->allContentTypes;
33+
}
34+
35+
public function getAllowedContentTypes(): array
36+
{
37+
return $this->allowedContentTypes;
38+
}
39+
}

bundle/Resources/config/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ services:
1919
- { name: monolog.logger, channel: ngscheduledvisibility }
2020

2121
Netgen\Bundle\IbexaScheduledVisibilityBundle\Service\ScheduledVisibilityService:
22+
arguments:
23+
- '@Netgen\Bundle\IbexaScheduledVisibilityBundle\Service\ScheduledVisibilityConfiguration'
24+
25+
Netgen\Bundle\IbexaScheduledVisibilityBundle\Service\ScheduledVisibilityConfiguration:
2226
arguments:
2327
- '@Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Registry'
2428
- '%netgen_ibexa_scheduled_visibility.type%'

bundle/Service/ScheduledVisibilityService.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@
99
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
1010
use Ibexa\Core\FieldType\Date\Value as DateValue;
1111
use Ibexa\Core\FieldType\DateAndTime\Value as DateAndTimeValue;
12+
use Netgen\Bundle\IbexaScheduledVisibilityBundle\Configuration\ScheduledVisibilityConfiguration;
1213
use Netgen\Bundle\IbexaScheduledVisibilityBundle\Enums\VisibilityUpdateResult;
13-
use Netgen\Bundle\IbexaScheduledVisibilityBundle\ScheduledVisibility\Registry;
1414
use OutOfBoundsException;
1515

1616
use function in_array;
1717

1818
final class ScheduledVisibilityService
1919
{
2020
public function __construct(
21-
private readonly Registry $registry,
22-
private readonly string $type,
23-
private readonly bool $enabled,
24-
private readonly bool $allContentTypes,
25-
private readonly array $allowedContentTypes,
21+
private ScheduledVisibilityConfiguration $configurationService,
2622
) {}
2723

2824
/**
@@ -34,7 +30,7 @@ public function updateVisibilityIfNeeded(Content $content): VisibilityUpdateResu
3430
return VisibilityUpdateResult::NoChange;
3531
}
3632

37-
$handler = $this->registry->get($this->type);
33+
$handler = $this->configurationService->getHandler();
3834

3935
if ($this->shouldBeHidden($content) && !$handler->isHidden($content)) {
4036
$handler->hide($content);
@@ -53,13 +49,13 @@ public function updateVisibilityIfNeeded(Content $content): VisibilityUpdateResu
5349

5450
public function accept(Content $content): bool
5551
{
56-
$enabled = $this->enabled;
57-
if (!$enabled) {
52+
if (!$this->configurationService->isEnabled()) {
5853
return false;
5954
}
6055

61-
$allowedAll = $this->allContentTypes;
62-
$allowedContentTypes = $this->allowedContentTypes;
56+
$allowedAll = $this->configurationService->isAllContentTypes();
57+
$allowedContentTypes = $this->configurationService->getAllowedContentTypes();
58+
6359
$contentType = $content->getContentType();
6460
if (!$allowedAll && !in_array($contentType->identifier, $allowedContentTypes, true)) {
6561
return false;

0 commit comments

Comments
 (0)