Skip to content

Commit

Permalink
Add getUrlsByType
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Oct 14, 2023
1 parent 9e715ae commit 790974e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
28 changes: 28 additions & 0 deletions src/Repository/UrlRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Result;
use FreshAdvance\Sitemap\DataStructure\Url;
use Generator;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;

class UrlRepository implements UrlRepositoryInterface
Expand Down Expand Up @@ -66,4 +67,31 @@ public function getUrl(string $objectId, string $objectType): ?Url

return null;
}

public function getUrlsByType(string $objectType, int $page, int $perPage): Generator
{
$queryBuilder = $this->queryBuilderFactory->create();
$queryBuilder->select('*')
->from('fa_sitemap')
->where('object_type = :object_type')
->setParameters([
'object_type' => $objectType,
])
->orderBy("id")
->setFirstResult(--$page * $perPage)
->setMaxResults($perPage);

/** @var Result $result */
$result = $queryBuilder->execute();

/** @var false|array<string, int|string|bool> $data */
while ($data = $result->fetchAssociative()) {
yield new Url(
location: (string)$data['location'],
lastModified: (string)$data['modified'],
changeFrequency: (string)$data['frequency'],
priority: (float)$data['priority'],
);
}
}
}
3 changes: 3 additions & 0 deletions src/Repository/UrlRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace FreshAdvance\Sitemap\Repository;

use FreshAdvance\Sitemap\DataStructure\Url;
use Generator;

interface UrlRepositoryInterface
{
public function addUrl(string $objectId, string $objectType, Url $urlData): void;

public function getUrl(string $objectId, string $objectType): ?Url;

public function getUrlsByType(string $objectType, int $page, int $perPage): Generator;
}
51 changes: 38 additions & 13 deletions tests/Integration/Repository/UrlRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,68 @@
namespace FreshAdvance\Sitemap\Tests\Integration\Repository;

use FreshAdvance\Sitemap\DataStructure\Url;
use FreshAdvance\Sitemap\DataStructure\UrlInterface;
use FreshAdvance\Sitemap\Repository\UrlRepository;
use FreshAdvance\Sitemap\Repository\UrlRepositoryInterface;
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;

class UrlRepositoryTest extends IntegrationTestCase
{
protected const EXISTING_OBJECT_ID = 'exampleObjectId';
protected const EXISTING_OBJECT_TYPE = 'exampleObjectType';

public function setUp(): void
public function testSaveAndThenGetUrl(): void
{
parent::setUp();

$sut = $this->getSut();

$objectId = 'exampleObjectId';
$objectType = 'exampleObjectType';

$sut->addUrl(
self::EXISTING_OBJECT_ID,
self::EXISTING_OBJECT_TYPE,
$objectId,
$objectType,
new Url(
location: 'someLocation',
lastModified: 'modifiedDate',
changeFrequency: 'frequency',
priority: 0.3
)
);
}

public function testGetUrl(): void
{
$sut = $this->getSut();
$url = $sut->getUrl(self::EXISTING_OBJECT_ID, self::EXISTING_OBJECT_TYPE);
$url = $sut->getUrl($objectId, $objectType);

$this->assertSame('someLocation', $url->getLocation());
$this->assertSame('modifiedDate', $url->getLastModified());
$this->assertSame('frequency', $url->getChangeFrequency());
$this->assertSame(0.3, $url->getPriority());
}

public function testGetUrlsByType(): void
{
$sut = $this->getSut();
$objectType = 'someType';

for ($i = 1; $i <= 10; $i++) {
$sut->addUrl(
'exampleObject' . $i,
$objectType,
new Url(
location: 'someLocation' . $i,
lastModified: 'modifiedDate',
changeFrequency: 'frequency',
priority: 0.3
)
);
}

$startId = 4;
$count = 0;
/** @var UrlInterface $oneUrlItem */
foreach ($sut->getUrlsByType($objectType, 2, 3) as $oneUrlItem) {
$this->assertSame('someLocation' . ($startId++), $oneUrlItem->getLocation());
$count++;
}

$this->assertSame(3, $count);
}

protected function getSut(): UrlRepository
{
return $this->get(UrlRepositoryInterface::class);
Expand Down

0 comments on commit 790974e

Please sign in to comment.