Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace all pagination with Filter classes #263

Merged
merged 5 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Controller/Admin/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buddy\Repman\Message\Organization\RemoveOrganization;
use Buddy\Repman\Query\Admin\OrganizationQuery;
use Buddy\Repman\Query\Filter;
use Buddy\Repman\Query\User\Model\Organization;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -26,9 +27,12 @@ public function __construct(OrganizationQuery $organizationQuery)
*/
public function list(Request $request): Response
{
$filter = Filter::fromRequest($request);

return $this->render('admin/organization/list.html.twig', [
'organizations' => $this->organizationQuery->findAll(20, (int) $request->get('offset', 0)),
'organizations' => $this->organizationQuery->findAll($filter),
'count' => $this->organizationQuery->count(),
'filter' => $filter,
]);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Controller/Admin/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buddy\Repman\Message\Proxy\RemoveDist;
use Buddy\Repman\Query\Admin\Proxy\DownloadsQuery;
use Buddy\Repman\Query\Filter;
use Buddy\Repman\Service\Proxy;
use Buddy\Repman\Service\Proxy\ProxyRegister;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -30,15 +31,18 @@ public function __construct(ProxyRegister $register, DownloadsQuery $downloadsQu
*/
public function list(string $proxy, Request $request): Response
{
$filter = Filter::fromRequest($request);

$packages = $this->register->getByHost($proxy)->syncedPackages();
$count = $packages->length();
$packages = $packages->drop((int) $request->get('offset', 0))->take(20)->iterator()->toArray();
$packages = $packages->drop($filter->getOffset())->take($filter->getLimit())->iterator()->toArray();

return $this->render('admin/proxy/dist.html.twig', [
'proxy' => $proxy,
'packages' => $packages,
'downloads' => $this->downloadsQuery->findByNames($packages),
'count' => $count,
'filter' => $filter,
]);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Controller/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Buddy\Repman\Message\User\EnableUser;
use Buddy\Repman\Query\Admin\Model\User;
use Buddy\Repman\Query\Admin\UserQuery;
use Buddy\Repman\Query\Filter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -29,9 +30,12 @@ public function __construct(UserQuery $userQuery)
*/
public function list(Request $request): Response
{
$filter = Filter::fromRequest($request);

return $this->render('admin/user/list.html.twig', [
'users' => $this->userQuery->findAll(20, (int) $request->get('offset', 0)),
'users' => $this->userQuery->findAll($filter),
'count' => $this->userQuery->count(),
'filter' => $filter,
]);
}

Expand Down
11 changes: 9 additions & 2 deletions src/Controller/Organization/MembersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Buddy\Repman\Message\Organization\Member\InviteUser;
use Buddy\Repman\Message\Organization\Member\RemoveInvitation;
use Buddy\Repman\Message\Organization\Member\RemoveMember;
use Buddy\Repman\Query\Filter;
use Buddy\Repman\Query\User\Model\Organization;
use Buddy\Repman\Query\User\Model\Organization\Member;
use Buddy\Repman\Query\User\OrganizationQuery;
Expand Down Expand Up @@ -39,11 +40,14 @@ public function __construct(OrganizationQuery $organizations, TokenStorageInterf
*/
public function listMembers(Organization $organization, Request $request): Response
{
$filter = Filter::fromRequest($request);

return $this->render('organization/member/members.html.twig', [
'organization' => $organization,
'members' => $this->organizations->findAllMembers($organization->id(), 20, (int) $request->get('offset', 0)),
'members' => $this->organizations->findAllMembers($organization->id(), $filter),
'count' => $this->organizations->membersCount($organization->id()),
'invitations' => $this->organizations->invitationsCount($organization->id()),
'filter' => $filter,
]);
}

Expand Down Expand Up @@ -102,10 +106,13 @@ public function invite(Organization $organization, Request $request): Response
*/
public function listInvitations(Organization $organization, Request $request): Response
{
$filter = Filter::fromRequest($request);

return $this->render('organization/member/invitations.html.twig', [
'organization' => $organization,
'invitations' => $this->organizations->findAllInvitations($organization->id(), 20, (int) $request->get('offset', 0)),
'invitations' => $this->organizations->findAllInvitations($organization->id(), $filter),
'count' => $this->organizations->invitationsCount($organization->id()),
'filter' => $filter,
]);
}

Expand Down
20 changes: 15 additions & 5 deletions src/Controller/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Buddy\Repman\Query\User\Model\Package;
use Buddy\Repman\Query\User\OrganizationQuery;
use Buddy\Repman\Query\User\PackageQuery;
use Buddy\Repman\Query\User\PackageQuery\Filter as PackageFilter;
use Buddy\Repman\Security\Model\User;
use Buddy\Repman\Service\ExceptionHandler;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function overview(Organization $organization): Response
*/
public function packages(Organization $organization, Request $request): Response
{
$filter = Filter::fromRequest($request);
$filter = PackageFilter::fromRequest($request);

$count = $this->packageQuery->count($organization->id(), $filter);

Expand Down Expand Up @@ -121,11 +122,14 @@ public function removePackage(Organization $organization, Package $package): Res
*/
public function packageDetails(Organization $organization, Package $package, Request $request): Response
{
$filter = Filter::fromRequest($request);

return $this->render('organization/package/details.html.twig', [
'organization' => $organization,
'package' => $package,
'filter' => $filter,
'count' => $this->packageQuery->versionCount($package->id()),
'versions' => $this->packageQuery->getVersions($package->id(), 20, (int) $request->get('offset', 0)),
'versions' => $this->packageQuery->getVersions($package->id(), $filter),
]);
}

Expand Down Expand Up @@ -217,10 +221,13 @@ public function generateToken(Organization $organization, Request $request): Res
*/
public function tokens(Organization $organization, Request $request): Response
{
$filter = Filter::fromRequest($request);

return $this->render('organization/tokens.html.twig', [
'tokens' => $this->organizationQuery->findAllTokens($organization->id(), 20, (int) $request->get('offset', 0)),
'tokens' => $this->organizationQuery->findAllTokens($organization->id(), $filter),
'count' => $this->organizationQuery->tokenCount($organization->id()),
'organization' => $organization,
'filter' => $filter,
]);
}

Expand Down Expand Up @@ -338,10 +345,13 @@ public function scanPackage(Organization $organization, Package $package): Respo
*/
public function packageScanResults(Organization $organization, Package $package, Request $request): Response
{
$filter = Filter::fromRequest($request);

return $this->render('organization/package/scanResults.html.twig', [
'organization' => $organization,
'package' => $package,
'results' => $this->packageQuery->getScanResults($package->id(), 20, (int) $request->get('offset', 0)),
'filter' => $filter,
'results' => $this->packageQuery->getScanResults($package->id(), $filter),
'count' => $this->packageQuery->getScanResultsCount($package->id()),
]);
}
Expand Down Expand Up @@ -373,6 +383,6 @@ private function hasNoPackages(Organization $organization): bool

return $user instanceof User &&
$organization->isOwner($user->id()) &&
$this->packageQuery->count($organization->id(), (new Filter())) === 0;
$this->packageQuery->count($organization->id(), new PackageFilter()) === 0;
}
}
3 changes: 2 additions & 1 deletion src/DataFixtures/PrivatePackageDownloadFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buddy\Repman\Entity\Organization\Package\Download;
use Buddy\Repman\Query\Admin\OrganizationQuery;
use Buddy\Repman\Query\Filter;
use Buddy\Repman\Query\User\Model\PackageName;
use Buddy\Repman\Query\User\PackageQuery;
use Doctrine\Bundle\FixturesBundle\Fixture;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function __construct(OrganizationQuery $organizations, PackageQuery $pack
public function load(ObjectManager $manager): void
{
$this->em->getConfiguration()->setSQLLogger(null);
foreach ($this->organizations->findAll(9999) as $organization) {
foreach ($this->organizations->findAll(new Filter(0, 100)) as $organization) {
foreach ($this->packages->getAllNames($organization->id()) as $package) {
$this->loadDownloads($package);
}
Expand Down
3 changes: 2 additions & 1 deletion src/DataFixtures/TokenFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Buddy\Repman\Message\Organization\GenerateToken;
use Buddy\Repman\Query\Admin\Model\Organization;
use Buddy\Repman\Query\Admin\OrganizationQuery;
use Buddy\Repman\Query\Filter;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
Expand Down Expand Up @@ -44,7 +45,7 @@ public function load(ObjectManager $manager): void
$progress->start();

$this->em->getConfiguration()->setSQLLogger(null);
foreach ($this->organizations->findAll(9999) as $organization) {
foreach ($this->organizations->findAll(new Filter(0, 100)) as $organization) {
$this->generateTokens($organization);
$progress->advance();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Query/Admin/OrganizationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace Buddy\Repman\Query\Admin;

use Buddy\Repman\Query\Admin\Model\Organization;
use Buddy\Repman\Query\Filter;
use Buddy\Repman\Query\User\Model\Installs;

interface OrganizationQuery
{
/**
* @return Organization[]
*/
public function findAll(int $limit = 20, int $offset = 0): array;
public function findAll(Filter $filter): array;

public function count(): int;

Expand Down
7 changes: 4 additions & 3 deletions src/Query/Admin/OrganizationQuery/DbalOrganizationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buddy\Repman\Query\Admin\Model\Organization;
use Buddy\Repman\Query\Admin\OrganizationQuery;
use Buddy\Repman\Query\Filter;
use Buddy\Repman\Query\User\Model\Installs;
use Doctrine\DBAL\Connection;

Expand All @@ -21,7 +22,7 @@ public function __construct(Connection $connection)
/**
* @return Organization[]
*/
public function findAll(int $limit = 20, int $offset = 0): array
public function findAll(Filter $filter): array
{
return array_map(function (array $data): Organization {
return $this->hydrateOrganization($data);
Expand All @@ -33,8 +34,8 @@ public function findAll(int $limit = 20, int $offset = 0): array
ORDER BY o.alias
LIMIT :limit OFFSET :offset',
[
':limit' => $limit,
':offset' => $offset,
':limit' => $filter->getLimit(),
':offset' => $filter->getOffset(),
])
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Query/Admin/UserQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace Buddy\Repman\Query\Admin;

use Buddy\Repman\Query\Admin\Model\User;
use Buddy\Repman\Query\Filter;
use Munus\Control\Option;

interface UserQuery
{
/**
* @return User[]
*/
public function findAll(int $limit = 20, int $offset = 0): array;
public function findAll(Filter $filter): array;

/**
* @return Option<User>
Expand Down
7 changes: 4 additions & 3 deletions src/Query/Admin/UserQuery/DbalUserQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buddy\Repman\Query\Admin\Model\User;
use Buddy\Repman\Query\Admin\UserQuery;
use Buddy\Repman\Query\Filter;
use Doctrine\DBAL\Connection;
use Munus\Control\Option;

Expand All @@ -18,13 +19,13 @@ public function __construct(Connection $connection)
$this->connection = $connection;
}

public function findAll(int $limit = 20, int $offset = 0): array
public function findAll(Filter $filter): array
{
return array_map(function (array $data): User {
return $this->hydrateUser($data);
}, $this->connection->fetchAll('SELECT id, email, status, roles FROM "user" ORDER BY email LIMIT :limit OFFSET :offset', [
':limit' => $limit,
':offset' => $offset,
':limit' => $filter->getLimit(),
':offset' => $filter->getOffset(),
]));
}

Expand Down
30 changes: 5 additions & 25 deletions src/Query/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

use Symfony\Component\HttpFoundation\Request;

final class Filter
class Filter
{
private int $offset = 0;
private int $limit = 20;
private ?string $searchTerm;

public function __construct(int $offset = 0, int $limit = 20, ?string $searchTerm = null)
public function __construct(int $offset = 0, int $limit = 20)
{
if ($offset >= 0) {
$this->offset = $offset;
Expand All @@ -25,8 +24,6 @@ public function __construct(int $offset = 0, int $limit = 20, ?string $searchTer
if ($this->limit > 100) {
$this->limit = 100;
}

$this->searchTerm = $searchTerm;
}

public function getOffset(): int
Expand All @@ -39,39 +36,22 @@ public function getLimit(): int
return $this->limit;
}

public function getSearchTerm(): ?string
{
return $this->searchTerm;
}

public function hasSearchTerm(): bool
{
return $this->searchTerm !== null;
}

/**
* @return array<string,string>
*/
public function getQueryStringParams(): array
{
$params = [
return [
'offset' => (string) $this->getOffset(),
'limit' => (string) $this->getLimit(),
];

if ($this->hasSearchTerm()) {
$params['search'] = (string) $this->getSearchTerm();
}

return $params;
}

public static function fromRequest(Request $request): Filter
public static function fromRequest(Request $request): self
{
return new self(
(int) $request->get('offset', 0),
(int) $request->get('limit', 20),
$request->get('search', null)
(int) $request->get('limit', 20)
);
}
}
Loading