Skip to content
Open
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "compucie/database",
"description": "Library of database managers used by Newton's systems.",
"version": "6.0.1",
"version": "6.1.0",
"type": "library",
"license": "MIT",
"autoload": {
Expand Down
83 changes: 0 additions & 83 deletions src/Poll/Model/Options.php

This file was deleted.

15 changes: 13 additions & 2 deletions src/Poll/Model/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
*/
readonly class Poll
{
/**
* @param int $id
* @param string $question
* @param DateTime $publishedAt
* @param DateTime $expiresAt
* @param array<Option> $options
* @param int $voteCount
*/
public function __construct(
private int $id,
private string $question,
private DateTime $publishedAt,
private DateTime $expiresAt,
private Options $options,
private array $options,
private int $voteCount,
) {
}
Expand All @@ -39,7 +47,10 @@ public function getExpiresAt(): DateTime
return $this->expiresAt;
}

public function getOptions(): Options
/**
* @return array<Option>
*/
public function getOptions(): array
{
return $this->options;
}
Expand Down
54 changes: 11 additions & 43 deletions src/Poll/PollDatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Compucie\Database\DatabaseManager;
use Compucie\Database\Poll\Model\Option;
use Compucie\Database\Poll\Model\Options;
use Compucie\Database\Poll\Model\Poll;
use Compucie\Database\Poll\Model\Vote;
use DateTime;
Expand Down Expand Up @@ -118,15 +117,14 @@ public function getPoll(int $pollId): ?Poll
}

$options = $this->getOptions($pollId);
$optionsVoteCounted = $this->getVoteCounts($pollId, $options);
$pollVoteCount = $this->getPollVoteCount($pollId);

return new Poll(
$pollId,
(string)$row['question'],
safeDateTime((string)$row['published_at']),
safeDateTime((string)$row['expires_at']),
$optionsVoteCounted,
$options,
$pollVoteCount
);
}
Expand Down Expand Up @@ -390,13 +388,13 @@ public function getOption(int $optionId): ?Option
/**
* Return the options for the given poll.
* @param int $pollId ID of the poll for which to get the options.
* @return Options Object with the options.
* @return array<Option> Object with the options.
* @throws mysqli_sql_exception
*/
public function getOptions(int $pollId): Options
public function getOptions(int $pollId): array
{
if ($pollId <= 0) {
return new Options();
return array();
}

$rows = $this->executeReadAll(
Expand All @@ -407,13 +405,16 @@ public function getOptions(int $pollId): Options
"i"
);

$options = new Options();
$options = array();

foreach ($rows as $row) {
$options->setText(
(int)$row['option_id'],
(string)$row['text']
$option = new Option(
$row['option_id'],
$pollId,
$row['text'],
$this->getVotes($row['option_id'])
);
$options[] = $option;
}

return $options;
Expand Down Expand Up @@ -448,39 +449,6 @@ public function deleteOption(int $optionId): bool
return $this->executeDelete('options', 'option_id', $optionId);
}

/**
* Return the options and the vote count for each option.
* The value is another array that contains the option text and the vote count.
* @param int $pollId ID of the poll for which to get the vote counts.
* @param Options $options Object with the options.
* @return Options Object with the options and their vote counts.
* @throws mysqli_sql_exception
*/
private function getVoteCounts(int $pollId, Options $options): Options
{
$rows = $this->executeReadAll(
"SELECT `option_id`, COUNT(*) AS cnt
FROM `votes`
WHERE `poll_id` = ?
GROUP BY `option_id`",
[$pollId],
"i"
);

foreach ($options->getIds() as $optionId) {
$options->setVoteCount($optionId, 0);
}

foreach ($rows as $row) {
$options->setVoteCount(
(int)$row['option_id'],
(int)$row['cnt']
);
}

return $options;
}

/* ------------- VOTES ------------- */

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/PollDatabaseManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,11 @@ function testGetPoll(): void
// echo $poll->getExpiresAt()->format('Y-m-d H:i:s');
assertSame($date1->format('Y-m-d H:i:s'), $poll->getExpiresAt()->format('Y-m-d H:i:s'));
$options = $poll->getOptions();
assertSame(2, count($options->getIds()));
assertSame($option1, $options->getText(1));
assertSame(2, $options->getVoteCount(1));
assertSame($option2, $options->getText(2));
assertSame(1, $options->getVoteCount(2));
assertSame(2, count($options));
assertSame($option1, $options[0]->getText());
assertSame(2, $options[0]->getVoteCount());
assertSame($option2, $options[1]->getText());
assertSame(1, $options[1]->getVoteCount());
assertSame(3, $poll->getVoteCount());
}

Expand Down