Skip to content

Commit

Permalink
Reworked IsBookmarked criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Aug 19, 2024
1 parent ab255a9 commit dfe7121
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Location;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator\Specifications;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Value\IsBookmarkedValue;
use Ibexa\Contracts\Core\Repository\Values\Filter\FilteringCriterion;

final class IsBookmarked extends Location implements FilteringCriterion
{
public function __construct(int $userId)
public function __construct(?int $userId = null)
{
parent::__construct(null, Operator::EQ, $userId);
$valueData = new IsBookmarkedValue($userId);

parent::__construct(
null,
Operator::EQ,
true,
$valueData
);
}

public function getSpecifications(): array
Expand All @@ -24,7 +32,7 @@ public function getSpecifications(): array
new Specifications(
Operator::EQ,
Specifications::FORMAT_SINGLE,
Specifications::TYPE_INTEGER
Specifications::TYPE_BOOLEAN
),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@ services:

Ibexa\Core\Search\Legacy\Content\Location\Gateway\CriterionHandler\Location\IsBookmarked:
parent: Ibexa\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler
arguments:
$connection: '@ibexa.persistence.connection'
$permissionResolver: '@Ibexa\Contracts\Core\Repository\PermissionResolver'
tags:
- { name: ibexa.search.legacy.gateway.criterion_handler.location }
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@
*/
namespace Ibexa\Core\Search\Legacy\Content\Location\Gateway\CriterionHandler\Location;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Types\Types;
use Ibexa\Contracts\Core\Exception\InvalidArgumentException;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase;
use Ibexa\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter;
use Ibexa\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler;

final class IsBookmarked extends CriterionHandler
{
private PermissionResolver $permissionResolver;

public function __construct(
Connection $connection,
PermissionResolver $permissionResolver
) {
parent::__construct($connection);

$this->permissionResolver = $permissionResolver;
}

public function accept(Criterion $criterion): bool
{
return $criterion instanceof Criterion\Location\IsBookmarked
Expand All @@ -23,13 +37,17 @@ public function accept(Criterion $criterion): bool

/**
* @param array{languages: string[]} $languageSettings
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
public function handle(
CriteriaConverter $converter,
QueryBuilder $queryBuilder,
Criterion $criterion,
array $languageSettings
) {
$userId = $this->getUserId($criterion);

$subQueryBuilder = $this->connection->createQueryBuilder();
$subQueryBuilder
->select(DoctrineDatabase::COLUMN_LOCATION_ID)
Expand All @@ -40,15 +58,37 @@ public function handle(
->eq(
DoctrineDatabase::COLUMN_USER_ID,
$queryBuilder->createNamedParameter(
$criterion->value[0],
$userId,
Types::INTEGER
)
)
);

return $queryBuilder->expr()->in(
't.node_id',
$subQueryBuilder->getSQL()
);
return $queryBuilder
->expr()
->in(
't.node_id',
$subQueryBuilder->getSQL()
);
}

/**
* @throws \Ibexa\Contracts\Core\Exception\InvalidArgumentException
*/
private function getUserId(Criterion $criterion): int
{
$valueData = $criterion->valueData;
if (!$valueData instanceof Criterion\Value\IsBookmarkedValue) {
throw new InvalidArgumentException(
'$criterion->valueData',
sprintf(
'Is expected to be of type: "%s", got "%s"',
Criterion\Value\IsBookmarkedValue::class,
get_debug_type($valueData)
)
);
}

return $valueData->getUserId() ?? $this->permissionResolver->getCurrentUserReference()->getUserId();
}
}
12 changes: 10 additions & 2 deletions tests/integration/Core/Repository/SearchServiceBookmarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ protected function setUp(): void
parent::setUp();

$this->addMediaFolderToBookmark();

$this->refreshSearch();
}

/**
Expand All @@ -36,7 +38,6 @@ public function testCriterion(
$query->filter = new Query\Criterion\LogicalAnd(
$criteria
);

$searchHits = self::getSearchService()->findLocations($query);

self::assertSame(
Expand Down Expand Up @@ -80,7 +81,14 @@ public function provideDataForTestCriterion(): iterable
1,
[
new Query\Criterion\ContentTypeIdentifier('user'),
new Query\Criterion\Location\IsBookmarked(self::ADMIN_USER_ID),
new Query\Criterion\Location\IsBookmarked(),
],
];

yield 'No bookmarked locations for user with id 10' => [
0,
[
new Query\Criterion\Location\IsBookmarked(10),
],
];
}
Expand Down

0 comments on commit dfe7121

Please sign in to comment.