-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-8726 and #417 Key changes: * Added loadUserIdsByLocation method to bookmark gateway * Added method loadUserIdsByLocation to bookmark handler * Added IsBookmarked criterion * Added IsBookmarked criterion handler for LSE * [Tests] Added integration tests for search using IsBookmarked criterion --------- Co-Authored-By: Konrad Oboza <konradoboza@users.noreply.github.com>
- Loading branch information
1 parent
f15e57c
commit 0ba8cd1
Showing
17 changed files
with
598 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/contracts/Repository/Values/Content/Query/Criterion/Location/IsBookmarked.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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 Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Location; | ||
|
||
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\Filter\FilteringCriterion; | ||
|
||
/** | ||
* This criterion only works for current user reference. | ||
*/ | ||
final class IsBookmarked extends Location implements FilteringCriterion | ||
{ | ||
public function __construct(bool $value = true) | ||
{ | ||
parent::__construct( | ||
null, | ||
Operator::EQ, | ||
$value, | ||
); | ||
} | ||
|
||
public function getSpecifications(): array | ||
{ | ||
return [ | ||
new Specifications( | ||
Operator::EQ, | ||
Specifications::FORMAT_SINGLE, | ||
Specifications::TYPE_BOOLEAN | ||
), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/lib/Search/Common/EventSubscriber/BookmarkEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?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 Ibexa\Core\Search\Common\EventSubscriber; | ||
|
||
use Ibexa\Contracts\Core\Repository\Events\Bookmark\CreateBookmarkEvent; | ||
use Ibexa\Contracts\Core\Repository\Events\Bookmark\DeleteBookmarkEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class BookmarkEventSubscriber extends AbstractSearchEventSubscriber implements EventSubscriberInterface | ||
{ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
CreateBookmarkEvent::class => ['onCreateBookmark', -100], | ||
DeleteBookmarkEvent::class => ['onDeleteBookmark', -100], | ||
]; | ||
} | ||
|
||
public function onCreateBookmark(CreateBookmarkEvent $event): void | ||
{ | ||
$location = $event->getLocation(); | ||
$this->updateContentIndex($location->getContentId()); | ||
$this->updateLocationIndex($location->getId()); | ||
} | ||
|
||
public function onDeleteBookmark(DeleteBookmarkEvent $event): void | ||
{ | ||
$location = $event->getLocation(); | ||
$this->updateContentIndex($location->getContentId()); | ||
$this->updateLocationIndex($location->getId()); | ||
} | ||
|
||
private function updateContentIndex(int $contentId): void | ||
{ | ||
$persistenceContent = $this->persistenceHandler->contentHandler()->load($contentId); | ||
|
||
$this->searchHandler->indexContent($persistenceContent); | ||
} | ||
|
||
private function updateLocationIndex(int $locationId): void | ||
{ | ||
$persistenceLocation = $this->persistenceHandler->locationHandler()->load($locationId); | ||
|
||
$this->searchHandler->indexLocation($persistenceLocation); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/lib/Search/Legacy/Content/Location/Gateway/CriterionHandler/Location/IsBookmarked.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?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 Ibexa\Core\Search\Legacy\Content\Location\Gateway\CriterionHandler\Location; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\ParameterType; | ||
use Doctrine\DBAL\Query\QueryBuilder; | ||
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; | ||
use LogicException; | ||
|
||
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 | ||
&& $criterion->operator === Criterion\Operator::EQ; | ||
} | ||
|
||
/** | ||
* @param array{languages: string[]} $languageSettings | ||
*/ | ||
public function handle( | ||
CriteriaConverter $converter, | ||
QueryBuilder $queryBuilder, | ||
Criterion $criterion, | ||
array $languageSettings | ||
) { | ||
if (!is_array($criterion->value)) { | ||
throw new LogicException(sprintf( | ||
'Expected %s Criterion value to be an array, %s received', | ||
IsBookmarked::class, | ||
get_debug_type($criterion->value), | ||
)); | ||
} | ||
|
||
$userId = $this->permissionResolver | ||
->getCurrentUserReference() | ||
->getUserId(); | ||
|
||
$subQueryBuilder = $this->connection->createQueryBuilder(); | ||
$subQueryBuilder | ||
->select('1') | ||
->from(DoctrineDatabase::TABLE_BOOKMARKS, 'b') | ||
->andWhere( | ||
$queryBuilder | ||
->expr() | ||
->eq( | ||
'b.' . DoctrineDatabase::COLUMN_USER_ID, | ||
$queryBuilder->createNamedParameter($userId, ParameterType::INTEGER) | ||
), | ||
$queryBuilder | ||
->expr() | ||
->eq('b.node_id', 't.node_id') | ||
); | ||
|
||
$query = 'EXISTS (%s)'; | ||
if (!$criterion->value[0]) { | ||
$query = 'NOT ' . $query; | ||
} | ||
|
||
return sprintf( | ||
$query, | ||
$subQueryBuilder->getSQL() | ||
); | ||
} | ||
} |
Oops, something went wrong.