2
0
Fork 0
forked from flashii/eeprom
eeprom-nabucco/src/Application.php
2020-05-08 22:53:21 +00:00

90 lines
2.4 KiB
PHP

<?php
namespace EEPROM;
use Exception;
use JsonSerializable;
class ApplicationNotFoundException extends Exception {}
final class Application implements JsonSerializable {
public function getId(): int {
return $this->app_id ?? 0;
}
public function getName(): string {
return $this->app_name ?? '';
}
public function getCreated(): int {
return $this->app_created ?? 0;
}
public function getSizeLimit(): int {
return $this->app_size_limit ?? -1;
}
public function allowSizeMultiplier(): bool {
return !empty($this->app_allow_size_multiplier);
}
public function getExpiry(): int {
return $this->app_expiry ?? -1;
}
public function jsonSerialize() {
return [
'id' => $this->getId(),
'name' => $this->getName(),
'size_limit' => $this->getSizeLimit(),
'size_multi' => $this->allowSizeMultiplier(),
'expiry' => $this->getExpiry(),
'created' => date('c', $this->getCreated()),
];
}
public static function byId(int $appId): self {
if($appId < 1)
throw new ApplicationNotFoundException;
$getApplication = DB::prepare('
SELECT `app_id`, `app_name`, `app_size_limit`, `app_expiry`, `app_allow_size_multiplier`,
UNIX_TIMESTAMP(`app_created`) AS `app_created`
FROM `prm_applications`
WHERE `app_id` = :app
');
$getApplication->bindValue('app', $appId);
$getApplication->execute();
$application = $getApplication->fetchObject(self::class);
if($application === false)
throw new ApplicationNotFoundException;
return $application;
}
public static function all(int $limit = 0, int $after = 0): array {
$query = '
SELECT `app_id`, `app_name`, `app_size_limit`, `app_expiry`, `app_allow_size_multiplier`,
UNIX_TIMESTAMP(`app_created`) AS `app_created`
FROM `prm_applications`
';
if($after > 0)
$query .= sprintf(' WHERE `app_id` > %d', $after);
$query .= ' ORDER BY `app_id`';
if($limit > 0)
$query .= sprintf(' LIMIT %d', $limit);
$getAppls = DB::prepare($query);
$getAppls->execute();
$out = [];
while($appl = $getAppls->fetchObject(self::class))
$out[] = $appl;
return $out;
}
}