-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Query; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use SpeedPuzzling\Web\Results\PuzzleOverview; | ||
|
||
readonly final class GetPuzzlesOverview | ||
{ | ||
public function __construct( | ||
private Connection $database, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<PuzzleOverview> | ||
*/ | ||
public function all(): array | ||
{ | ||
$query = <<<SQL | ||
SELECT puzzle.name AS puzzle_name, puzzle.pieces_count, manufacturer.name AS manufacturer_name, AVG(puzzle_solving_time.seconds_to_solve) AS average_time, COUNT(puzzle_solving_time.id) AS solved_count, MIN(puzzle_solving_time.seconds_to_solve) AS fastest_time | ||
FROM puzzle | ||
LEFT JOIN puzzle_solving_time ON puzzle_solving_time.puzzle_id = puzzle.id | ||
INNER JOIN manufacturer ON puzzle.manufacturer_id = manufacturer.id | ||
WHERE approved = true | ||
GROUP BY puzzle.name, puzzle.pieces_count, manufacturer.name | ||
HAVING MIN(puzzle_solving_time.seconds_to_solve) > 0 | ||
ORDER BY puzzle.name ASC | ||
SQL; | ||
|
||
$data = $this->database | ||
->executeQuery($query) | ||
->fetchAllAssociative(); | ||
|
||
return array_map(static function(array $row): PuzzleOverview { | ||
/** | ||
* @var array{ | ||
* puzzle_name: string, | ||
* manufacturer_name: string, | ||
* pieces_count: int, | ||
* average_time: string, | ||
* fastest_time: int, | ||
* solved_count: int | ||
* } $row | ||
*/ | ||
|
||
return PuzzleOverview::fromDatabaseRow($row); | ||
}, $data); | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Results; | ||
|
||
readonly final class PuzzleOverview | ||
{ | ||
public function __construct( | ||
public string $puzzleName, | ||
public string $manufacturerName, | ||
public int $piecesCount, | ||
public int $averageTime, | ||
public int $fastestTime, | ||
public int $solvedCount, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array{ | ||
* puzzle_name: string, | ||
* manufacturer_name: string, | ||
* pieces_count: int, | ||
* average_time: string, | ||
* fastest_time: int, | ||
* solved_count: int, | ||
* } $row | ||
*/ | ||
public static function fromDatabaseRow(array $row): self | ||
{ | ||
return new self( | ||
puzzleName: $row['puzzle_name'], | ||
manufacturerName: $row['manufacturer_name'], | ||
piecesCount: $row['pieces_count'], | ||
averageTime: (int) $row['average_time'], | ||
fastestTime: $row['fastest_time'], | ||
solvedCount: $row['solved_count'], | ||
); | ||
} | ||
} |
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