-
Notifications
You must be signed in to change notification settings - Fork 19
IBX-12029: Added content publication strategy contract with synchronous default strategy #781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
04544ac
IBX-12029: Added content publication strategy contract with synchrono…
bnowak 06fe63b
IBX-12029: Added unit tests for content publication strategies
bnowak 80a9a6e
IBX-12029: Added improvements
bnowak 174cfa9
IBX-12029: Improved phpstan languages type
bnowak fa6aa37
IBX-12029: Addressed CR remarks
bnowak 295e436
IBX-12029: Removed not needed comment
bnowak bd7df84
IBX-12029: Introduced ContentPublicationResult
bnowak e44db5a
IBX-12029: Added @experimental phpdoc to ContentPublicationResult
bnowak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/contracts/Repository/Strategy/ContentPublication/ContentPublicationResult.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Contracts\Core\Repository\Strategy\ContentPublication; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Values\Content\Content; | ||
|
|
||
| /** | ||
| * @experimental This contract may change in future. | ||
| */ | ||
| final readonly class ContentPublicationResult | ||
| { | ||
| public function __construct( | ||
| public ?Content $publishedContent, | ||
| ) { | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/contracts/Repository/Strategy/ContentPublication/ContentPublicationStrategyInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Contracts\Core\Repository\Strategy\ContentPublication; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Values\Content\Language; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; | ||
|
|
||
| /** | ||
| * Entry point for publishing a content version, hiding whether the publication happens | ||
| * synchronously (in-request) or asynchronously (queued background work). | ||
| * | ||
| * Strategies are registered with the "ibexa.repository.content.publication_strategy" service tag | ||
| * and consulted in priority order; the first one supporting the current repository executes. | ||
| */ | ||
| interface ContentPublicationStrategyInterface | ||
| { | ||
| public function supports(): bool; | ||
|
|
||
| /** | ||
| * @param list<string> $translations List of language codes of translations which will be | ||
| * included in a published version | ||
| * | ||
| * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException | ||
| * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException | ||
| */ | ||
| public function publishVersion(VersionInfo $versionInfo, array $translations = Language::ALL): ContentPublicationResult; | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
src/lib/Repository/Strategy/ContentPublication/ChainContentPublicationStrategy.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Core\Repository\Strategy\ContentPublication; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Strategy\ContentPublication\ContentPublicationResult; | ||
| use Ibexa\Contracts\Core\Repository\Strategy\ContentPublication\ContentPublicationStrategyInterface; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Language; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; | ||
| use LogicException; | ||
|
|
||
| /** | ||
| * Dispatches publication to the first supporting strategy, in priority order. | ||
| * Exactly one strategy executes per publication. | ||
| * | ||
| * @internal Meant for internal use by Repository | ||
| */ | ||
| final readonly class ChainContentPublicationStrategy implements ContentPublicationStrategyInterface | ||
| { | ||
| /** | ||
| * @param iterable<ContentPublicationStrategyInterface> $strategies | ||
| */ | ||
| public function __construct( | ||
| private iterable $strategies, | ||
| ) { | ||
| } | ||
|
|
||
| public function supports(): bool | ||
| { | ||
| foreach ($this->strategies as $strategy) { | ||
| if ($strategy->supports()) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public function publishVersion(VersionInfo $versionInfo, array $translations = Language::ALL): ContentPublicationResult | ||
| { | ||
| foreach ($this->strategies as $strategy) { | ||
| if ($strategy->supports()) { | ||
| return $strategy->publishVersion($versionInfo, $translations); | ||
| } | ||
| } | ||
|
|
||
| throw new LogicException(sprintf( | ||
| 'No content publication strategy supports the current publication. At least %s' | ||
| . ' must be tagged with "ibexa.repository.content.publication_strategy".', | ||
| SynchronousContentPublicationStrategy::class, | ||
| )); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/lib/Repository/Strategy/ContentPublication/SynchronousContentPublicationStrategy.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Core\Repository\Strategy\ContentPublication; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\ContentService; | ||
| use Ibexa\Contracts\Core\Repository\Strategy\ContentPublication\ContentPublicationResult; | ||
| use Ibexa\Contracts\Core\Repository\Strategy\ContentPublication\ContentPublicationStrategyInterface; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Language; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; | ||
|
|
||
| /** | ||
| * Synchronous publication strategy: publishes the version inside the current request. | ||
| * Always-applicable fallback, registered with the lowest priority. | ||
| * | ||
| * @internal | ||
| */ | ||
| final readonly class SynchronousContentPublicationStrategy implements ContentPublicationStrategyInterface | ||
| { | ||
| public function __construct( | ||
| private ContentService $contentService, | ||
| ) { | ||
| } | ||
|
|
||
| public function supports(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| public function publishVersion(VersionInfo $versionInfo, array $translations = Language::ALL): ContentPublicationResult | ||
| { | ||
| return new ContentPublicationResult( | ||
| $this->contentService->publishVersion($versionInfo, $translations) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| services: | ||
| _defaults: | ||
| public: false | ||
| autoconfigure: true | ||
| autowire: true | ||
|
|
||
| # Content publication strategies. The first strategy (in priority order) whose supports() | ||
| # returns true executes; the synchronous strategy is the always-applicable fallback. | ||
| # Additional strategies (e.g. asynchronous publication) are provided by dedicated packages | ||
| # via the "ibexa.repository.content.publication_strategy" tag with a higher priority. | ||
| Ibexa\Core\Repository\Strategy\ContentPublication\SynchronousContentPublicationStrategy: | ||
| arguments: | ||
| $contentService: '@ibexa.api.service.content' | ||
| tags: | ||
| - { name: ibexa.repository.content.publication_strategy, priority: -100 } | ||
|
|
||
| Ibexa\Core\Repository\Strategy\ContentPublication\ChainContentPublicationStrategy: | ||
| arguments: | ||
| $strategies: !tagged_iterator ibexa.repository.content.publication_strategy | ||
|
|
||
| Ibexa\Contracts\Core\Repository\Strategy\ContentPublication\ContentPublicationStrategyInterface: | ||
| alias: Ibexa\Core\Repository\Strategy\ContentPublication\ChainContentPublicationStrategy |
106 changes: 106 additions & 0 deletions
106
tests/lib/Repository/Strategy/Publication/ChainContentPublicationStrategyTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\Core\Repository\Strategy\Publication; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Strategy\ContentPublication\ContentPublicationResult; | ||
| use Ibexa\Contracts\Core\Repository\Strategy\ContentPublication\ContentPublicationStrategyInterface; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; | ||
| use Ibexa\Core\Repository\Strategy\ContentPublication\ChainContentPublicationStrategy; | ||
| use LogicException; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class ChainContentPublicationStrategyTest extends TestCase | ||
| { | ||
| public function testPublishVersionExecutesFirstSupportingStrategy(): void | ||
| { | ||
| $versionInfo = $this->createStub(VersionInfo::class); | ||
| $result = new ContentPublicationResult(null); | ||
|
|
||
| $notSupportingStrategy = $this->createMock(ContentPublicationStrategyInterface::class); | ||
| $notSupportingStrategy | ||
| ->expects(self::once()) | ||
| ->method('supports') | ||
| ->willReturn(false); | ||
| $notSupportingStrategy | ||
| ->expects(self::never()) | ||
| ->method('publishVersion'); | ||
|
|
||
| $supportingStrategy = $this->createMock(ContentPublicationStrategyInterface::class); | ||
| $supportingStrategy | ||
| ->expects(self::once()) | ||
| ->method('supports') | ||
| ->willReturn(true); | ||
| $supportingStrategy | ||
| ->expects(self::once()) | ||
| ->method('publishVersion') | ||
| ->with(self::identicalTo($versionInfo), ['eng-GB']) | ||
| ->willReturn($result); | ||
|
|
||
| $neverConsultedStrategy = $this->createMock(ContentPublicationStrategyInterface::class); | ||
| $neverConsultedStrategy | ||
| ->expects(self::never()) | ||
| ->method('supports'); | ||
| $neverConsultedStrategy | ||
| ->expects(self::never()) | ||
| ->method('publishVersion'); | ||
|
|
||
| $chain = new ChainContentPublicationStrategy([ | ||
| $notSupportingStrategy, | ||
| $supportingStrategy, | ||
| $neverConsultedStrategy, | ||
| ]); | ||
|
|
||
| self::assertSame($result, $chain->publishVersion($versionInfo, ['eng-GB'])); | ||
| } | ||
|
|
||
| public function testSupportsReturnsTrueWhenAnyStrategySupports(): void | ||
| { | ||
| $notSupportingStrategy = $this->createMock(ContentPublicationStrategyInterface::class); | ||
| $notSupportingStrategy | ||
| ->expects(self::once()) | ||
| ->method('supports') | ||
| ->willReturn(false); | ||
|
|
||
| $supportingStrategy = $this->createMock(ContentPublicationStrategyInterface::class); | ||
| $supportingStrategy | ||
| ->expects(self::once()) | ||
| ->method('supports') | ||
| ->willReturn(true); | ||
|
|
||
| $chain = new ChainContentPublicationStrategy([$notSupportingStrategy, $supportingStrategy]); | ||
|
|
||
| self::assertTrue($chain->supports()); | ||
| } | ||
|
|
||
| public function testSupportsReturnsFalseForEmptyChain(): void | ||
| { | ||
| $chain = new ChainContentPublicationStrategy([]); | ||
|
|
||
| self::assertFalse($chain->supports()); | ||
| } | ||
|
|
||
| public function testPublishVersionThrowsLogicExceptionWhenNoStrategySupports(): void | ||
| { | ||
| $notSupportingStrategy = $this->createMock(ContentPublicationStrategyInterface::class); | ||
| $notSupportingStrategy | ||
| ->expects(self::once()) | ||
| ->method('supports') | ||
| ->willReturn(false); | ||
| $notSupportingStrategy | ||
| ->expects(self::never()) | ||
| ->method('publishVersion'); | ||
|
|
||
| $chain = new ChainContentPublicationStrategy([$notSupportingStrategy]); | ||
|
|
||
| $this->expectException(LogicException::class); | ||
| $this->expectExceptionMessage('No content publication strategy supports the current publication.'); | ||
|
|
||
| $chain->publishVersion($this->createMock(VersionInfo::class)); | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
tests/lib/Repository/Strategy/Publication/SynchronousContentPublicationStrategyTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\Core\Repository\Strategy\Publication; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\ContentService; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Content; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Language; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; | ||
| use Ibexa\Core\Repository\Strategy\ContentPublication\SynchronousContentPublicationStrategy; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class SynchronousContentPublicationStrategyTest extends TestCase | ||
| { | ||
| public function testSupportsAlwaysReturnsTrue(): void | ||
| { | ||
| $strategy = new SynchronousContentPublicationStrategy( | ||
| $this->createStub(ContentService::class) | ||
| ); | ||
|
|
||
| self::assertTrue($strategy->supports()); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider providerForTestPublishVersionDelegatesToContentService | ||
| * | ||
| * @param list<string>|null $translations | ||
| * @param list<string> $expectedTranslations | ||
| */ | ||
| public function testPublishVersionDelegatesToContentService( | ||
| ?array $translations, | ||
| array $expectedTranslations | ||
| ): void { | ||
| $versionInfo = $this->createStub(VersionInfo::class); | ||
| $content = $this->createStub(Content::class); | ||
|
|
||
| $contentService = $this->createMock(ContentService::class); | ||
| $contentService | ||
| ->expects(self::once()) | ||
| ->method('publishVersion') | ||
| ->with(self::identicalTo($versionInfo), $expectedTranslations) | ||
| ->willReturn($content); | ||
|
|
||
| $publishArguments = ['versionInfo' => $versionInfo]; | ||
| if (null !== $translations) { | ||
| $publishArguments['translations'] = $translations; | ||
| } | ||
|
|
||
| $result = (new SynchronousContentPublicationStrategy($contentService)) | ||
| ->publishVersion(...$publishArguments); | ||
|
|
||
| self::assertSame($content, $result->publishedContent); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{list<string>|null, list<string>}> | ||
| */ | ||
| public static function providerForTestPublishVersionDelegatesToContentService(): iterable | ||
| { | ||
| yield 'explicit translations' => [['ger-DE'], ['ger-DE']]; | ||
| yield 'defaults to all translations' => [null, Language::ALL]; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.