Skip to content

Commit

Permalink
IBX-4469: Fixed add content based parameters to request (#111)
Browse files Browse the repository at this point in the history
* Add content based parameters to request when contextItems is passed

* Bumped phpstan version

* Fixed errors reported by phpstan

* Fixed cs
  • Loading branch information
ciastektk authored Dec 8, 2022
1 parent 717b37c commit 6b0ef31
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 223 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
],
"require": {
"ext-json": "*",
"php": "^7.3 || ^8.0",
"ezsystems/doctrine-dbal-schema": "^1.0@dev",
"ezsystems/ezplatform-content-forms": "^1.3@dev",
Expand All @@ -26,9 +27,9 @@
"dg/bypass-finals": "^1.1",
"ezsystems/ezplatform-code-style": "^0.4",
"ezsystems/ezplatform-http-cache": "^2.3@dev",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^0.12.85",
"phpstan/phpstan-phpunit": "^0.12.18",
"phpstan/phpstan": "^1.2",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-symfony": "^1.0",
"phpunit/phpunit": "^8.5"
},
"autoload": {
Expand Down
411 changes: 218 additions & 193 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- phpstan-baseline.neon

parameters:
ignoreErrors:
- "#^Cannot call method (log|debug|info|notice|warning|error|critical|alert|emergency)\\(\\) on Psr\\\\Log\\\\LoggerInterface\\|null\\.$#"
level: max
level: 8
paths:
- src
- tests
7 changes: 1 addition & 6 deletions src/lib/Client/EzRecommendationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use EzSystems\EzRecommendationClient\API\AbstractAPI;
use EzSystems\EzRecommendationClient\Config\CredentialsResolverInterface;
use EzSystems\EzRecommendationClient\Exception\BadAPICallException;
use EzSystems\EzRecommendationClient\Exception\BadResponseException;
use EzSystems\EzRecommendationClient\Exception\CredentialsNotFoundException;
use EzSystems\EzRecommendationClient\Factory\EzRecommendationClientAPIFactory;
Expand Down Expand Up @@ -207,11 +206,7 @@ public function getHttpClient(): ClientInterface
*/
public function __call(string $name, array $arguments): AbstractAPI
{
try {
return $this->eZRecommendationClientApiFactory->buildAPI($name, $this);
} catch (BadAPICallException $exception) {
$this->logger->error(self::ERROR_MESSAGE . $exception->getMessage());
}
return $this->eZRecommendationClientApiFactory->buildAPI($name, $this);
}

/**
Expand Down
30 changes: 14 additions & 16 deletions src/lib/Event/Subscriber/RecommendationEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,27 @@ public function onRecommendationResponse(RecommendationResponseEvent $event): vo
$event->setRecommendationItems($this->extractRecommendationItems($response));
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function getRecommendationRequest(ParameterBag $parameterBag): RecommendationRequest
{
$contextItem = null;
$content = $parameterBag->get(Request::CONTEXT_ITEMS_KEY);
if ($content instanceof Content) {
$contextItem = $this->repositoryConfigResolver->useRemoteId() ? $content->contentInfo->remoteId : $content->id;
}

return new Request([
$parameters = [
RecommendationRequest::SCENARIO => $parameterBag->get(RecommendationRequest::SCENARIO, ''),
Request::LIMIT_KEY => $parameterBag->get(Request::LIMIT_KEY, 3),
Request::CONTEXT_ITEMS_KEY => $contextItem,
Request::CONTENT_TYPE_KEY => $content->getContentType()->id,
Request::OUTPUT_TYPE_ID_KEY => $parameterBag->get(Request::OUTPUT_TYPE_ID_KEY),
Request::CATEGORY_PATH_KEY => $this->getCategoryPath($content),
Request::LANGUAGE_KEY => $this->getRequestLanguage($parameterBag->get(self::LOCALE_REQUEST_KEY)),
Request::ATTRIBUTES_KEY => $parameterBag->get(Request::ATTRIBUTES_KEY, []),
Request::FILTERS_KEY => $parameterBag->get(Request::FILTERS_KEY, []),
]);
];

$content = $parameterBag->get(Request::CONTEXT_ITEMS_KEY);
if ($content instanceof Content) {
$parameters[Request::CONTEXT_ITEMS_KEY] = $this->repositoryConfigResolver->useRemoteId()
? $content->contentInfo->remoteId
: $content->id;
$parameters[Request::CONTENT_TYPE_KEY] = $content->getContentType()->id;
$parameters[Request::CATEGORY_PATH_KEY] = $this->getCategoryPath($content);
}

return new Request($parameters);
}

private function getRequestLanguage(?string $locale): string
Expand All @@ -115,7 +113,7 @@ private function extractRecommendationItems(ResponseInterface $response): array

$recommendations = $response->getBody()->getContents();

$recommendationItems = json_decode($recommendations, true);
$recommendationItems = json_decode($recommendations, true, 512, JSON_THROW_ON_ERROR);

return $this->recommendationService->getRecommendationItems($recommendationItems['recommendationItems']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,13 @@ private function getIntro(Content $content): string

if ($value instanceof RichTextValue) {
return $value->xml->textContent;
} elseif ($value instanceof TextLineValue) {
}

if ($value instanceof TextLineValue) {
return $value->text;
}

return (string) $value;
}

private function getImage(Content $content): ?string
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Field/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ private function getImageFieldIdentifier($contentId, string $language, bool $rel
if (!empty($field->destinationContentId)) {
return $this->getImageFieldIdentifier($field->destinationContentId, $language, true);
}
} else {
return $this->getConfiguredFieldIdentifier('image', $contentType);
}

return $this->getConfiguredFieldIdentifier('image', $contentType);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/SPI/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class Request
{
/**
* @param \EzSystems\EzRecommendationClient\SPI\Request $instance
* @param string[] $parameters
* @param array<string, mixed> $parameters
*/
public function __construct(self $instance, array $parameters = [])
{
Expand Down

0 comments on commit 6b0ef31

Please sign in to comment.