2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-10-07 11:47:30 +03:00
|
|
|
namespace openvk\Web\Models\Entities;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:47:30 +03:00
|
|
|
use Chandler\Database\DatabaseConnection as DB;
|
|
|
|
use openvk\Web\Models\Repositories\Users;
|
|
|
|
use openvk\Web\Models\RowModel;
|
|
|
|
|
|
|
|
class Voucher extends RowModel
|
|
|
|
{
|
|
|
|
protected $tableName = "coin_vouchers";
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getCoins(): int
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->coins;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getRating(): int
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->rating;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getToken(): string
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->token;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getFormattedToken(): string
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
$fmtTok = "";
|
2021-10-07 11:47:30 +03:00
|
|
|
$token = $this->getRecord()->token;
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach (array_chunk(str_split($token), 6) as $chunk) {
|
2021-10-07 11:47:30 +03:00
|
|
|
$fmtTok .= implode("", $chunk) . "-";
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 11:47:30 +03:00
|
|
|
return substr($fmtTok, 0, -1);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getRemainingUsages(): float
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
|
|
|
return (float) ($this->getRecord()->usages_left ?? INF);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUsers(int $page = -1, ?int $perPage = null): \Traversable
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
|
|
|
$relations = $this->getRecord()->related("voucher_users.voucher");
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($page !== -1) {
|
2021-10-07 11:47:30 +03:00
|
|
|
$relations = $relations->page($page, $perPage ?? OPENVK_DEFAULT_PER_PAGE);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($relations as $relation) {
|
|
|
|
yield (new Users())->get($relation->user);
|
|
|
|
}
|
2021-10-07 11:47:30 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function isExpired(): bool
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
|
|
|
return $this->getRemainingUsages() < 1;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function wasUsedBy(User $user): bool
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
|
|
|
$record = $this->getRecord()->related("voucher_users.voucher")->where("user", $user->getId());
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:47:30 +03:00
|
|
|
return sizeof($record) > 0;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function willUse(User $user): bool
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($this->wasUsedBy($user)) {
|
2021-10-07 11:47:30 +03:00
|
|
|
return false;
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isExpired()) {
|
2021-10-07 11:47:30 +03:00
|
|
|
return false;
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 11:47:30 +03:00
|
|
|
$this->setRemainingUsages($this->getRemainingUsages() - 1);
|
|
|
|
DB::i()->getContext()->table("voucher_users")->insert([
|
|
|
|
"voucher" => $this->getId(),
|
|
|
|
"user" => $user->getId(),
|
|
|
|
]);
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:47:30 +03:00
|
|
|
return true;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function setRemainingUsages(float $usages): void
|
2021-10-07 11:47:30 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
$this->stateChanges("usages_left", $usages === INF ? null : ((int) $usages));
|
2021-10-07 11:47:30 +03:00
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
}
|