Skip to content

Commit

Permalink
Refactored ContentTypeHelper (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk authored Apr 28, 2022
1 parent 3f7b2ff commit 5ef4262
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 67 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/bundle/Templating/Twig/Functions/UserTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/Helper/ContentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
62 changes: 7 additions & 55 deletions src/lib/Helper/ContentTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
2 changes: 1 addition & 1 deletion src/lib/Service/EventNotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 9 additions & 1 deletion tests/lib/Service/EventNotificationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
]
),
])
);
}
}

0 comments on commit 5ef4262

Please sign in to comment.