2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-10-11 19:04:43 +03:00
|
|
|
namespace openvk\ServiceAPI;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-10-11 19:04:43 +03:00
|
|
|
use Chandler\MVC\Routing\Router;
|
|
|
|
use openvk\Web\Models\Entities\User;
|
|
|
|
use openvk\Web\Models\Exceptions\{AlreadyVotedException, InvalidOptionException, PollLockedException};
|
|
|
|
use openvk\Web\Models\Repositories\Polls as PollRepo;
|
|
|
|
use UnexpectedValueException;
|
|
|
|
|
|
|
|
class Polls implements Handler
|
|
|
|
{
|
|
|
|
protected $user;
|
|
|
|
protected $polls;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function __construct(?User $user)
|
2022-10-11 19:04:43 +03:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
2025-01-31 18:20:13 +03:00
|
|
|
$this->polls = new PollRepo();
|
2022-10-11 19:04:43 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-10-11 19:04:43 +03:00
|
|
|
private function getPollHtml(int $poll): string
|
|
|
|
{
|
|
|
|
return Router::i()->execute("/poll$poll", "SAPI");
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function vote(int $pollId, string $options, callable $resolve, callable $reject): void
|
2022-10-11 19:04:43 +03:00
|
|
|
{
|
|
|
|
$poll = $this->polls->get($pollId);
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$poll) {
|
2022-10-11 19:04:43 +03:00
|
|
|
$reject("Poll not found");
|
|
|
|
return;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-10-11 19:04:43 +03:00
|
|
|
try {
|
|
|
|
$options = explode(",", $options);
|
|
|
|
$poll->vote($this->user, $options);
|
2025-01-31 18:20:13 +03:00
|
|
|
} catch (AlreadyVotedException $ex) {
|
2022-10-11 19:04:43 +03:00
|
|
|
$reject("Poll state changed: user has already voted.");
|
|
|
|
return;
|
2025-01-31 18:20:13 +03:00
|
|
|
} catch (PollLockedException $ex) {
|
2022-10-11 19:04:43 +03:00
|
|
|
$reject("Poll state changed: poll has ended.");
|
|
|
|
return;
|
2025-01-31 18:20:13 +03:00
|
|
|
} catch (InvalidOptionException $ex) {
|
2022-10-11 19:04:43 +03:00
|
|
|
$reject("Foreign options passed.");
|
|
|
|
return;
|
2025-01-31 18:20:13 +03:00
|
|
|
} catch (UnexpectedValueException $ex) {
|
2022-10-11 19:04:43 +03:00
|
|
|
$reject("Too much options passed.");
|
|
|
|
return;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-10-11 19:04:43 +03:00
|
|
|
$resolve(["html" => $this->getPollHtml($pollId)]);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function unvote(int $pollId, callable $resolve, callable $reject): void
|
2022-10-11 19:04:43 +03:00
|
|
|
{
|
|
|
|
$poll = $this->polls->get($pollId);
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$poll) {
|
2022-10-11 19:04:43 +03:00
|
|
|
$reject("Poll not found");
|
|
|
|
return;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-10-11 19:04:43 +03:00
|
|
|
try {
|
|
|
|
$poll->revokeVote($this->user);
|
2025-01-31 18:20:13 +03:00
|
|
|
} catch (PollLockedException $ex) {
|
2022-10-11 19:04:43 +03:00
|
|
|
$reject("Votes can't be revoked from this poll.");
|
|
|
|
return;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-10-11 19:04:43 +03:00
|
|
|
$resolve(["html" => $this->getPollHtml($pollId)]);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|