Skip to content

Commit

Permalink
IBX-2334: Added remoteId support in export and update processes (#106)
Browse files Browse the repository at this point in the history
* Moved Export and Content Controllers to REST directory

* Fixed download export file route

* Added remoteId support for updated content url

* Added remoteId support in export

* Dropped useless ListParamConverter

* Added ContentQueryType

* Dropped useless property

* Added usage of LoggerAwareInterface in ContentHelper

* Removed redundand phpdoc

* Added methods to get query based on remoteId or numeric id

* Renamed methods to retrieve a query
  • Loading branch information
ciastektk authored Feb 17, 2022
1 parent ee85d79 commit 8e44b7b
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 238 deletions.
34 changes: 2 additions & 32 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,15 @@ parameters:
count: 1
path: src/bundle/Command/UserAttributesUpdateCommand.php

-
message: "#^PHPDoc tag @param has invalid value \\(\\\\Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag ParameterBag \\$parameterBag\\)\\: Unexpected token \"ParameterBag\", expected variable at offset 65$#"
count: 1
path: src/bundle/Controller/ContentController.php

-
message: "#^Parameter \\#1 \\$string of static method EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ParamsConverterHelper\\:\\:getArrayFromString\\(\\) expects string, bool\\|float\\|int\\|string\\|null given\\.$#"
count: 1
path: src/bundle/Controller/ContentController.php

-
message: "#^Property EzSystems\\\\EzRecommendationClientBundle\\\\Controller\\\\ContentController\\:\\:\\$searchService \\(eZ\\\\Publish\\\\Core\\\\Repository\\\\SearchService\\) does not accept eZ\\\\Publish\\\\API\\\\Repository\\\\SearchService\\.$#"
count: 1
path: src/bundle/Controller/ContentController.php

-
message: "#^Property EzSystems\\\\EzRecommendationClient\\\\SPI\\\\Content\\:\\:\\$fields \\(array\\<string\\>\\) does not accept array\\|null\\.$#"
count: 1
path: src/bundle/Controller/ContentController.php

-
message: "#^Property EzSystems\\\\EzRecommendationClient\\\\SPI\\\\Content\\:\\:\\$lang \\(string\\|null\\) does not accept bool\\|float\\|int\\|string\\|null\\.$#"
count: 1
path: src/bundle/Controller/ContentController.php

-
message: "#^Call to an undefined method EzSystems\\\\EzRecommendationClient\\\\Authentication\\\\AuthenticatorInterface\\:\\:authenticateByFile\\(\\)\\.$#"
count: 1
path: src/bundle/Controller/ExportController.php
path: src/bundle/Controller/REST/ExportController.php

-
message: "#^Parameter \\#2 \\$values of method Symfony\\\\Component\\\\HttpFoundation\\\\ResponseHeaderBag\\:\\:set\\(\\) expects array\\<string\\>\\|string\\|null, int\\|false given\\.$#"
count: 1
path: src/bundle/Controller/ExportController.php
path: src/bundle/Controller/REST/ExportController.php

-
message: "#^Method EzSystems\\\\EzRecommendationClientBundle\\\\Controller\\\\RecommendationController\\:\\:getTemplate\\(\\) should return string but returns string\\|null\\.$#"
Expand Down Expand Up @@ -690,11 +665,6 @@ parameters:
count: 1
path: src/lib/Helper/ContentHelper.php

-
message: "#^Interface eZ\\\\Publish\\\\API\\\\Repository\\\\SearchService referenced with incorrect case\\: eZ\\\\Publish\\\\Api\\\\Repository\\\\SearchService\\.$#"
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
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ includes:
- phpstan-baseline.neon

parameters:
ignoreErrors:
- "#^Cannot call method (log|debug|info|notice|warning|error|critical|alert|emergency)\\(\\) on Psr\\\\Log\\\\LoggerInterface\\|null\\.$#"
level: max
paths:
- src
Expand Down
106 changes: 0 additions & 106 deletions src/bundle/Controller/ContentController.php

This file was deleted.

110 changes: 110 additions & 0 deletions src/bundle/Controller/REST/ContentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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 EzSystems\EzRecommendationClientBundle\Controller\REST;

use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\Query;
use EzSystems\EzPlatformRest\Server\Controller as RestController;
use EzSystems\EzPlatformRest\Server\Exceptions\AuthenticationFailedException;
use EzSystems\EzRecommendationClient\Authentication\AuthenticatorInterface;
use EzSystems\EzRecommendationClient\Service\ContentServiceInterface;
use EzSystems\EzRecommendationClient\Value\Content;
use EzSystems\EzRecommendationClient\Value\ContentData;
use Ibexa\Personalization\QueryType\ContentQueryType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class ContentController extends RestController
{
/** @var \EzSystems\EzRecommendationClient\Authentication\AuthenticatorInterface */
private $authenticator;

/** @var \Ibexa\Personalization\QueryType\ContentQueryType */
private $contentQueryType;

/** @var \EzSystems\EzRecommendationClient\Service\ContentServiceInterface */
private $contentService;

/** @var \eZ\Publish\API\Repository\SearchService */
private $searchService;

public function __construct(
AuthenticatorInterface $authenticator,
ContentQueryType $contentQueryType,
ContentServiceInterface $contentService,
Repository $repository,
SearchService $searchService
) {
$this->authenticator = $authenticator;
$this->contentQueryType = $contentQueryType;
$this->contentService = $contentService;
$this->repository = $repository;
$this->searchService = $searchService;
}

/**
* @throws \Exception
*/
public function getContentByIdAction(int $contentId, Request $request): ContentData
{
if (!$this->authenticator->authenticate()) {
throw new AuthenticationFailedException('Access denied: wrong credentials', Response::HTTP_UNAUTHORIZED);
}

$requestQuery = $request->query;
$contentItems = $this->getContentItems(
$this->contentQueryType->getQueryByContentId($contentId, (string) $requestQuery->get('lang'))
);

return $this->getContentData($contentItems);
}

/**
* @throws \Exception
*/
public function getContentByRemoteIdAction(string $remoteId, Request $request): ContentData
{
if (!$this->authenticator->authenticate()) {
throw new AuthenticationFailedException('Access denied: wrong credentials', Response::HTTP_UNAUTHORIZED);
}

$requestQuery = $request->query;
$contentItems = $this->getContentItems(
$this->contentQueryType->getQueryByContentRemoteId($remoteId, (string) $requestQuery->get('lang'))
);

return $this->getContentData($contentItems);
}

/**
* @return array<\eZ\Publish\API\Repository\Values\Content\Search\SearchHit>
*
* @throws \Exception
*/
private function getContentItems(Query $query): array
{
return $this->repository->sudo(function () use ($query) {
return $this->searchService->findContent($query)->searchHits;
});
}

/**
* @param array<\eZ\Publish\API\Repository\Values\Content\Search\SearchHit> $contentItems
*/
private function getContentData(array $contentItems): ContentData
{
$contentData = $this->contentService->prepareContent(
[$contentItems],
new Content()
);

return new ContentData($contentData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
declare(strict_types=1);

namespace EzSystems\EzRecommendationClientBundle\Controller;
namespace EzSystems\EzRecommendationClientBundle\Controller\REST;

use EzSystems\EzPlatformRest\Server\Controller;
use EzSystems\EzRecommendationClient\Authentication\AuthenticatorInterface;
Expand Down
52 changes: 0 additions & 52 deletions src/bundle/ParamConverter/ListParamConverter.php

This file was deleted.

22 changes: 16 additions & 6 deletions src/bundle/Resources/config/routing_rest.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
ez_recommendation.content_type.get_content:
path: /ez_recommendation/v1/content/{idList}
ez_recommendation.rest.content.get_by_id:
path: /ez_recommendation/v1/content/id/{contentId}
defaults:
_controller: 'EzSystems\EzRecommendationClientBundle\Controller\ContentController::getContentAction'
_controller: 'EzSystems\EzRecommendationClientBundle\Controller\REST\ContentController::getContentByIdAction'
methods: [GET]
requirements:
contentId: '\d+'

ez_recommendation.rest.content.get_by_remote_id:
path: /ez_recommendation/v1/content/remote-id/{remoteId}
defaults:
_controller: 'EzSystems\EzRecommendationClientBundle\Controller\REST\ContentController::getContentByRemoteIdAction'
requirements:
remoteId: '[a-zA-Z0-9\_\-\/]+'
methods: [GET]

ez_recommendation.export.download:
path: /ez_recommendation/v1/exportDownload/{filePath}
ez_recommendation.rest.export.download:
path: /ez_recommendation/v1/export/download/{filePath}
defaults:
_controller: 'EzSystems\EzRecommendationClientBundle\Controller\ExportController::downloadAction'
_controller: 'EzSystems\EzRecommendationClientBundle\Controller\REST\ExportController::downloadAction'
requirements:
filePath: '[a-zA-Z0-9\_\-\/]+'
2 changes: 1 addition & 1 deletion src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ imports:
- { resource: services/commands.yaml }
- { resource: services/config.yaml }
- { resource: services/controllers.yaml }
- { resource: services/converters.yaml }
- { resource: services/events.yaml }
- { resource: services/exporter.yaml }
- { resource: services/factory.yaml }
Expand All @@ -15,6 +14,7 @@ imports:
- { resource: services/helpers.yaml }
- { resource: services/http.yaml }
- { resource: services/mappers.yaml }
- { resource: services/query_types.yaml }
- { resource: services/response.yaml }
- { resource: services/services.yaml }
- { resource: services/twig.yaml }
Expand Down
Loading

0 comments on commit 8e44b7b

Please sign in to comment.