Skip to content

Commit

Permalink
Merge branch '1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Jul 15, 2021
2 parents 49728df + e55da4b commit 9cedd02
Show file tree
Hide file tree
Showing 17 changed files with 733 additions and 2 deletions.
99 changes: 99 additions & 0 deletions eZ/Publish/API/Repository/Iterator/BatchIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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 eZ\Publish\API\Repository\Iterator;

use Iterator;

final class BatchIterator implements Iterator
{
public const DEFAULT_BATCH_SIZE = 25;

/** @var \eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter */
private $adapter;

/** @var Iterator|null */
private $innerIterator;

/** @var int */
private $batchSize;

/** @var int */
private $position;

public function __construct(
BatchIteratorAdapter $adapter,
int $batchSize = self::DEFAULT_BATCH_SIZE
) {
$this->adapter = $adapter;
$this->batchSize = $batchSize;
$this->position = 0;
}

public function current()
{
if (!$this->isInitialized()) {
$this->initialize();
}

return $this->innerIterator->current();
}

public function next(): void
{
if (!$this->isInitialized()) {
$this->initialize();
}

++$this->position;
$this->innerIterator->next();
if (!$this->innerIterator->valid() && ($this->position % $this->batchSize) === 0) {
$this->innerIterator = $this->fetch();
}
}

public function key(): int
{
return $this->position;
}

public function valid(): bool
{
if (!$this->isInitialized()) {
$this->initialize();
}

return $this->innerIterator->valid();
}

public function rewind(): void
{
$this->initialize();
}

public function setBatchSize(int $batchSize): void
{
$this->batchSize = $batchSize;
}

private function initialize(): void
{
$this->position = 0;
$this->innerIterator = $this->fetch();
}

private function isInitialized(): bool
{
return isset($this->innerIterator);
}

private function fetch(): Iterator
{
return $this->adapter->fetch($this->position, $this->batchSize);
}
}
16 changes: 16 additions & 0 deletions eZ/Publish/API/Repository/Iterator/BatchIteratorAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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 eZ\Publish\API\Repository\Iterator;

use Iterator;

interface BatchIteratorAdapter
{
public function fetch(int $offset, int $limit): Iterator;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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 eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter;

use eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
use Iterator;

abstract class AbstractSearchAdapter implements BatchIteratorAdapter
{
/** @var \eZ\Publish\API\Repository\SearchService */
protected $searchService;

/** @var \eZ\Publish\API\Repository\Values\Content\Query */
protected $query;

/** @var string[] */
protected $languageFilter;

/** @var bool */
protected $filterOnUserPermissions;

public function __construct(
SearchService $searchService,
Query $query,
array $languageFilter = [],
bool $filterOnUserPermissions = true
) {
$this->searchService = $searchService;
$this->query = $query;
$this->languageFilter = $languageFilter;
$this->filterOnUserPermissions = $filterOnUserPermissions;
}

final public function fetch(int $offset, int $limit): Iterator
{
$query = clone $this->query;
$query->offset = $offset;
$query->limit = $limit;

return $this->executeSearch($query)->getIterator();
}

abstract protected function executeSearch(Query $query): SearchResult;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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 eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter;
use eZ\Publish\API\Repository\Values\Filter\Filter;
use Iterator;

final class ContentFilteringAdapter implements BatchIteratorAdapter
{
/** @var \eZ\Publish\API\Repository\ContentService */
private $contentService;

/** @var \eZ\Publish\API\Repository\Values\Filter\Filter */
private $filter;

/** @var string[]|null */
private $languages;

public function __construct(ContentService $contentService, Filter $filter, ?array $languages = null)
{
$this->contentService = $contentService;
$this->filter = $filter;
$this->languages = $languages;
}

public function fetch(int $offset, int $limit): Iterator
{
$filter = clone $this->filter;
$filter->sliceBy($limit, $offset);

return $this->contentService->find($filter, $this->languages)->getIterator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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 eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter;

use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;

final class ContentInfoSearchAdapter extends AbstractSearchAdapter
{
protected function executeSearch(Query $query): SearchResult
{
return $this->searchService->findContentInfo(
$query,
$this->languageFilter,
$this->filterOnUserPermissions
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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 eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter;

use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;

final class ContentSearchAdapter extends AbstractSearchAdapter
{
protected function executeSearch(Query $query): SearchResult
{
return $this->searchService->findContent(
$query,
$this->languageFilter,
$this->filterOnUserPermissions
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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 eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter;

use eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Filter\Filter;
use Iterator;

final class LocationFilteringAdapter implements BatchIteratorAdapter
{
/** @var \eZ\Publish\API\Repository\LocationService */
private $locationService;

/** @var \eZ\Publish\API\Repository\Values\Filter\Filter */
private $filter;

/** @var string[]|null */
private $languages;

public function __construct(LocationService $locationService, Filter $filter, ?array $languages = null)
{
$this->locationService = $locationService;
$this->filter = $filter;
$this->languages = $languages;
}

public function fetch(int $offset, int $limit): Iterator
{
$filter = clone $this->filter;
$filter->sliceBy($limit, $offset);

return $this->locationService->find($filter, $this->languages)->getIterator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter;

use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;

final class LocationSearchAdapter extends AbstractSearchAdapter
{
public function __construct(
SearchService $searchService,
LocationQuery $query,
array $languageFilter = [],
bool $filterOnUserPermissions = true
) {
parent::__construct($searchService, $query, $languageFilter, $filterOnUserPermissions);
}

protected function executeSearch(Query $query): SearchResult
{
return $this->searchService->findLocations(
$query,
$this->languageFilter,
$this->filterOnUserPermissions
);
}
}
Loading

0 comments on commit 9cedd02

Please sign in to comment.