From 7ef99b6d70d9f27d0b69c7acbac5b599ec978182 Mon Sep 17 00:00:00 2001 From: Flavio Heleno Date: Sun, 20 Mar 2022 11:43:43 -0300 Subject: [PATCH] Improve Popular Package listing based on 'require'/'require-dev' count --- .../Action/Package/ViewPackageBadgeAction.php | 2 +- .../Package/PackageRepositoryInterface.php | 2 +- src/Domain/Stats/StatsRepositoryInterface.php | 2 + .../Package/PdoPackageRepository.php | 38 ++++++++----------- .../Persistence/Stats/PdoStatsRepository.php | 34 +++++++++++------ .../Version/PdoVersionRepository.php | 9 ----- 6 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/Application/Action/Package/ViewPackageBadgeAction.php b/src/Application/Action/Package/ViewPackageBadgeAction.php index 311bd1df..7398f60e 100644 --- a/src/Application/Action/Package/ViewPackageBadgeAction.php +++ b/src/Application/Action/Package/ViewPackageBadgeAction.php @@ -120,7 +120,7 @@ static function (Dependency $dependency): bool { case VersionStatusEnum::NoDeps: $status = [ - 'text' => 'no deps', + 'text' => 'none', 'color' => 'blue' ]; break; diff --git a/src/Domain/Package/PackageRepositoryInterface.php b/src/Domain/Package/PackageRepositoryInterface.php index 5e28c80e..22da2a13 100644 --- a/src/Domain/Package/PackageRepositoryInterface.php +++ b/src/Domain/Package/PackageRepositoryInterface.php @@ -8,7 +8,7 @@ public function create(string $name): Package; public function all(): PackageCollection; - public function findPopular(): PackageCollection; + public function findPopular(int $limit = 10): PackageCollection; public function exists(string $name): bool; diff --git a/src/Domain/Stats/StatsRepositoryInterface.php b/src/Domain/Stats/StatsRepositoryInterface.php index 487b40d3..a98ea7e5 100644 --- a/src/Domain/Stats/StatsRepositoryInterface.php +++ b/src/Domain/Stats/StatsRepositoryInterface.php @@ -19,6 +19,8 @@ public function create( public function all(): StatsCollection; + public function findPopular(): StatsCollection; + public function exists(string $packageName): bool; /** diff --git a/src/Infrastructure/Persistence/Package/PdoPackageRepository.php b/src/Infrastructure/Persistence/Package/PdoPackageRepository.php index 0b5d837b..0f28c61e 100644 --- a/src/Infrastructure/Persistence/Package/PdoPackageRepository.php +++ b/src/Infrastructure/Persistence/Package/PdoPackageRepository.php @@ -51,9 +51,6 @@ public function create(string $name): Package { ); } - /** - * {@inheritdoc} - */ public function all(): PackageCollection { static $stmt = null; if ($stmt === null) { @@ -74,34 +71,37 @@ public function all(): PackageCollection { return $packageCol; } - /** - * {@inheritdoc} - */ - public function findPopular(): PackageCollection { + public function findPopular(int $limit = 10): PackageCollection { static $stmt = null; if ($stmt === null) { $stmt = $this->pdo->query( <<fetch(PDO::FETCH_ASSOC)) { - $packageCol->add($this->hydrate($row)); + while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) && $packageCol->count() < $limit) { + if ($this->exists($row['name']) === false) { + continue; + } + + $packageCol->add($this->get($row['name'])); } return $packageCol; } - /** - * {@inheritdoc} - */ public function exists(string $name): bool { static $stmt = null; if ($stmt === null) { @@ -119,9 +119,6 @@ public function exists(string $name): bool { return $stmt->rowCount() === 1; } - /** - * {@inheritdoc} - */ public function get(string $name): Package { static $stmt = null; if ($stmt === null) { @@ -144,9 +141,6 @@ public function get(string $name): Package { return $this->hydrate($row); } - /** - * {@inheritdoc} - */ public function find(array $query): PackageCollection { $where = []; foreach (array_keys($query) as $col) { diff --git a/src/Infrastructure/Persistence/Stats/PdoStatsRepository.php b/src/Infrastructure/Persistence/Stats/PdoStatsRepository.php index c404e7eb..21073c07 100644 --- a/src/Infrastructure/Persistence/Stats/PdoStatsRepository.php +++ b/src/Infrastructure/Persistence/Stats/PdoStatsRepository.php @@ -80,9 +80,6 @@ public function create( ); } - /** - * {@inheritdoc} - */ public function all(): StatsCollection { static $stmt = null; if ($stmt === null) { @@ -103,9 +100,28 @@ public function all(): StatsCollection { return $statsCol; } - /** - * {@inheritdoc} - */ + public function findPopular(): StatsCollection { + static $stmt = null; + if ($stmt === null) { + $stmt = $this->pdo->query( + <<fetch(PDO::FETCH_ASSOC)) { + $statsCol->add($this->hydrate($row)); + } + + return $statsCol; + } + public function exists(string $packageName): bool { static $stmt = null; if ($stmt === null) { @@ -124,9 +140,6 @@ public function exists(string $packageName): bool { return $stmt->rowCount() === 1; } - /** - * {@inheritdoc} - */ public function get(string $packageName): Stats { static $stmt = null; if ($stmt === null) { @@ -149,9 +162,6 @@ public function get(string $packageName): Stats { return $this->hydrate($row); } - /** - * {@inheritdoc} - */ public function find(array $query): StatsCollection { $where = []; foreach (array_keys($query) as $col) { diff --git a/src/Infrastructure/Persistence/Version/PdoVersionRepository.php b/src/Infrastructure/Persistence/Version/PdoVersionRepository.php index 52352b2b..ae6a27af 100644 --- a/src/Infrastructure/Persistence/Version/PdoVersionRepository.php +++ b/src/Infrastructure/Persistence/Version/PdoVersionRepository.php @@ -64,16 +64,10 @@ public function create( ); } - /** - * {@inheritdoc} - */ public function all(): VersionCollection { return new VersionCollection(); } - /** - * {@inheritdoc} - */ public function get(int $id): Version { static $stmt = null; if ($stmt === null) { @@ -96,9 +90,6 @@ public function get(int $id): Version { return $this->hydrate($row); } - /** - * {@inheritdoc} - */ public function find(array $query): VersionCollection { $where = []; $cols = array_keys($query);