2
0
Fork 0
forked from flashii/eeprom
eeprom-nabucco/src/User.php

76 lines
1.9 KiB
PHP

<?php
namespace EEPROM;
use RuntimeException;
use Index\Data\IDbConnection;
class User {
private static $active;
public static function hasActive(): bool {
return !empty(self::$active);
}
public static function active(): self {
return self::$active;
}
public function __construct(
private $id = 0,
private $sizeMultiplier = 0,
private $created = 0,
private $restricted = 0,
) {}
public function __destruct() {
if($this === self::$active)
self::$active = null;
}
public function setActive(): self {
self::$active = $this;
return $this;
}
public function getId(): int {
return $this->id;
}
public function getSizeMultiplier(): int {
return $this->sizeMultiplier;
}
public function getCreated(): int {
return $this->created;
}
public function getRestricted(): int {
return $this->restricted;
}
public function isRestricted(): bool {
return $this->restricted > 0;
}
public static function byId(IDbConnection $conn, int $userId): self {
$create = $conn->prepare('INSERT IGNORE INTO `prm_users` (`user_id`) VALUES (?)');
$create->addParameter(1, $userId);
$create->execute();
$get = $conn->prepare(
'SELECT `user_id`, `user_size_multiplier`, UNIX_TIMESTAMP(`user_created`) AS `user_created`,'
. ' UNIX_TIMESTAMP(`user_restricted`) AS `user_restricted` FROM `prm_users` WHERE `user_id` = ?'
);
$get->addParameter(1, $userId);
$get->execute();
$result = $get->getResult();
if(!$result->next())
throw new RuntimeException('User not found.');
return new User(
$result->getInteger(0),
$result->getInteger(1),
$result->getInteger(2),
$result->getInteger(3),
);
}
}