Skip to content

Commit

Permalink
More improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Oct 24, 2023
1 parent 6efdc5b commit c61c781
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 51 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
},
"autoload-dev": {
"psr-4": {
"Ibexa\\Tests\\Search\\": "tests/lib/",
"Ibexa\\Tests\\Bundle\\Search\\": "tests/bundle/",
"Ibexa\\Tests\\Contracts\\Search\\": "tests/contracts/",
"Ibexa\\Tests\\Search\\": "tests/lib/",
"Ibexa\\Platform\\Tests\\Contracts\\Search\\": "tests/contracts/",
"Ibexa\\Platform\\Tests\\Bundle\\Search\\": "tests/bundle/",
"Ibexa\\Platform\\Tests\\Search\\": "tests/lib/"
}
Expand Down
17 changes: 9 additions & 8 deletions src/bundle/Controller/SuggestionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@
namespace Ibexa\Bundle\Search\Controller;

use Ibexa\Search\Model\SuggestionQuery;
use Ibexa\Search\Serializer\SuggestionSerializer;
use Ibexa\Search\Service\SuggestionService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\SerializerInterface;

final class SuggestionController extends AbstractController
{
private SuggestionService $suggestionService;

private SerializerInterface $serializer;

public function __construct(
SerializerInterface $serializer,
SuggestionService $suggestionService
) {
$this->suggestionService = $suggestionService;
$this->serializer = $serializer;
}

public static function getSubscribedServices(): array
{
return array_merge(parent::getSubscribedServices(), [
'serializer' => SuggestionSerializer::class,
]);
}

public function suggestAction(SuggestionQuery $suggestionQuery): JsonResponse
{
$result = $this->suggestionService->suggest($suggestionQuery);

$serializedResults = $this->serializer->serialize($result, 'json');

return (new JsonResponse(null, 200))->setJson($serializedResults);
return $this->json($result);
}
}
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ services:
Ibexa\Search\Provider\ParentLocationProvider: ~

Ibexa\Contracts\Search\Provider\ParentLocationProvider: '@Ibexa\Search\Provider\ParentLocationProvider'

Ibexa\Search\Serializer\SuggestionSerializer: ~
38 changes: 0 additions & 38 deletions src/lib/Serializer/Normalizer/ContentSuggestionNormalizer.php

This file was deleted.

35 changes: 32 additions & 3 deletions src/lib/Serializer/SuggestionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,46 @@

namespace Ibexa\Search\Serializer;

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

final class SuggestionSerializer implements SerializerInterface
{
public function serialize($data, string $format, array $context = [])
private SerializerInterface $serializer;

public function __construct()
{
$encoders = [new JsonEncoder()];

$defaultContext = [
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => static function ($object) {
return $object->getId();
},
];

$normalizers = [new ObjectNormalizer(null, null, null, null, null, null, $defaultContext)];

$this->serializer = new Serializer($normalizers, $encoders);
}

/**
* @param mixed $data
* @param array<mixed> $context
*/
public function serialize($data, string $format, array $context = []): string
{
// TODO: Implement serialize() method.
return $this->serializer->serialize($data, $format, $context);
}

/**
* @param mixed $data
* @param array<mixed> $context
*/
public function deserialize($data, string $type, string $format, array $context = [])
{
// TODO: Implement deserialize() method.
return $this->serializer->deserialize($data, $type, $format, $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Ibexa\Search\Model\SuggestionQuery;
use PHPUnit\Framework\TestCase;

class SuggestionEventTest extends TestCase
final class SuggestionEventTest extends TestCase
{
public function testConstruct(): void
{
Expand Down

0 comments on commit c61c781

Please sign in to comment.