diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c3fbbc36..21245d4a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -660,11 +660,6 @@ parameters: count: 1 path: src/lib/Generator/ContentListElementGenerator.php - - - message: "#^Cannot access property \\$contentInfo on eZ\\\\Publish\\\\API\\\\Repository\\\\Values\\\\Content\\\\Content\\|null\\.$#" - count: 1 - path: src/lib/Helper/ContentHelper.php - - message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper\\:\\:countContentItemsByContentTypeId\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#" count: 1 diff --git a/src/bundle/Templating/Twig/Functions/UserTracking.php b/src/bundle/Templating/Twig/Functions/UserTracking.php index 838f71d4..213fa0b8 100644 --- a/src/bundle/Templating/Twig/Functions/UserTracking.php +++ b/src/bundle/Templating/Twig/Functions/UserTracking.php @@ -65,17 +65,17 @@ public function __construct( */ public function trackUser(Content $content): ?string { - $contentInfo = $content->contentInfo; - if ($this->contentTypeHelper->isContentTypeExcluded($contentInfo)) { + $contentType = $content->getContentType(); + if ($this->contentTypeHelper->isContentTypeExcluded($contentType)) { return null; } - $contentId = $this->repositoryConfigResolver->useRemoteId() ? $contentInfo->remoteId : $content->id; + $contentId = $this->repositoryConfigResolver->useRemoteId() ? $content->contentInfo->remoteId : $content->id; return $this->render( [ 'contentId' => $contentId, - 'contentTypeId' => $content->getContentType()->id, + 'contentTypeId' => $contentType->id, ] ); } diff --git a/src/lib/Helper/ContentHelper.php b/src/lib/Helper/ContentHelper.php index b736bf5a..b5f6e257 100644 --- a/src/lib/Helper/ContentHelper.php +++ b/src/lib/Helper/ContentHelper.php @@ -125,8 +125,11 @@ public function getContent(int $contentId, ?array $languages = null, ?int $versi public function getIncludedContent(int $contentId, ?array $languages = null, ?int $versionNo = null): ?Content { $content = $this->getContent($contentId, $languages, $versionNo); + if (null === $content) { + return null; + } - return !$this->contentTypeHelper->isContentTypeExcluded($content->contentInfo) ? $content : null; + return !$this->contentTypeHelper->isContentTypeExcluded($content->getContentType()) ? $content : null; } /** diff --git a/src/lib/Helper/ContentTypeHelper.php b/src/lib/Helper/ContentTypeHelper.php index 945a376f..d837cbbd 100644 --- a/src/lib/Helper/ContentTypeHelper.php +++ b/src/lib/Helper/ContentTypeHelper.php @@ -8,77 +8,29 @@ namespace EzSystems\EzRecommendationClient\Helper; -use eZ\Publish\API\Repository\ContentService as ContentServiceInterface; -use eZ\Publish\API\Repository\ContentTypeService as ContentTypeServiceInterface; -use eZ\Publish\API\Repository\Repository as RepositoryInterface; -use eZ\Publish\API\Repository\Values\Content\ContentInfo; +use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\Core\MVC\ConfigResolverInterface; use EzSystems\EzRecommendationClient\Value\Parameters; final class ContentTypeHelper { - /** @var \eZ\Publish\API\Repository\ContentTypeService */ - private $contentTypeService; - - /** @var \eZ\Publish\API\Repository\ContentService */ - private $contentService; - /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */ private $configResolver; - /** @var \eZ\Publish\API\Repository\Repository */ - private $repository; - - public function __construct( - ContentTypeServiceInterface $contentTypeService, - ContentServiceInterface $contentService, - RepositoryInterface $repository, - ConfigResolverInterface $configResolver - ) { - $this->contentTypeService = $contentTypeService; - $this->contentService = $contentService; - $this->configResolver = $configResolver; - $this->repository = $repository; - } - - /** - * Returns ContentType ID based on $contentType name. - * - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - */ - public function getContentTypeId(string $contentType): int + public function __construct(ConfigResolverInterface $configResolver) { - return $this->contentTypeService->loadContentTypeByIdentifier($contentType)->id; - } - - /** - * Returns ContentType identifier based on $contentId. - * - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException - */ - public function getContentTypeIdentifier(int $contentId): string - { - return $this->contentTypeService->loadContentType( - $this->contentService - ->loadContent($contentId) - ->contentInfo - ->contentTypeId - )->identifier; + $this->configResolver = $configResolver; } /** * @throws \Exception */ - public function isContentTypeExcluded(ContentInfo $contentInfo): bool + public function isContentTypeExcluded(ContentType $contentType): bool { - $contentType = $this->repository->sudo(function () use ($contentInfo) { - return $this->contentTypeService->loadContentType($contentInfo->contentTypeId); - }); - - return !\in_array( + return !in_array( $contentType->identifier, - $this->configResolver->getParameter('included_content_types', Parameters::NAMESPACE) + $this->configResolver->getParameter('included_content_types', Parameters::NAMESPACE), + true ); } } diff --git a/src/lib/Service/EventNotificationService.php b/src/lib/Service/EventNotificationService.php index fb5bbabf..d0437f52 100644 --- a/src/lib/Service/EventNotificationService.php +++ b/src/lib/Service/EventNotificationService.php @@ -59,7 +59,7 @@ public function sendNotification(string $method, string $action, ContentInfo $co { $credentials = $this->clientCredentials->getCredentials(); - if (!$credentials || $this->contentTypeHelper->isContentTypeExcluded($contentInfo)) { + if (!$credentials || $this->contentTypeHelper->isContentTypeExcluded($contentInfo->getContentType())) { return; } diff --git a/tests/lib/Service/EventNotificationServiceTest.php b/tests/lib/Service/EventNotificationServiceTest.php index 141c6be2..52e65637 100644 --- a/tests/lib/Service/EventNotificationServiceTest.php +++ b/tests/lib/Service/EventNotificationServiceTest.php @@ -9,6 +9,7 @@ namespace EzSystems\EzRecommendationClient\Tests\Service; use eZ\Publish\API\Repository\Values\Content\ContentInfo; +use eZ\Publish\Core\Repository\Values\ContentType\ContentType; use EzSystems\EzRecommendationClient\API\Notifier; use EzSystems\EzRecommendationClient\Config\ExportCredentialsResolver; use EzSystems\EzRecommendationClient\Config\EzRecommendationClientCredentialsResolver; @@ -92,7 +93,14 @@ public function testSendNotification() $this->notificationService->sendNotification( 'onHideLocation', EventNotification::ACTION_UPDATE, - new ContentInfo([]) + new ContentInfo([ + 'contentType' => new ContentType( + [ + 'id' => 1, + 'identifier' => 'foo', + ] + ), + ]) ); } }