1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Exceptions\AccountNotFoundException;
- use app\Exceptions\ViewTrackerNotFoundException;
- use app\Interfaces\IDatabaseObject;
- use app\Hajeebtok;
- use app\Logger;
- use app\Exceptions\SecurityFaultException;
- class ViewTracker implements IDatabaseObject
- {
- public private(set) ?int $id;
- public private(set) ?int $video_id;
- public private(set) ?int $tracking_start;
- public private(set) ?string $ip;
- public private(set) ?int $account_id;
- public function __construct(?int $id = null, ?int $video_id = null, ?int $tracking_start = null, ?string $ip = null, ?int $account_id = null)
- {
- $this->id = $id;
- $this->video_id = $video_id;
- $this->tracking_start = $tracking_start;
- $this->ip = $ip;
- $this->account_id = $account_id;
- }
- /**
- * Creates the table for the object type in the database.
- */
- public static function CreateTable(): void
- {
- throw new SecurityFaultException("Attempt to create table on view tracker object.");
- }
- /**
- * Drops the table for the object type from the database.
- */
- public static function DropTable(): void
- {
- throw new SecurityFaultException("Attempt to drop table on view tracker object.");
- }
- /**
- * Saves the object to the database.
- */
- public function Save(): int
- {
- Hajeebtok::$Database->Query("INSERT INTO view_tracking (video_id, ip, account_id) VALUES (:video_id, :ip, :account_id)", [
- "video_id" => $this->video_id,
- "ip" => $this->ip,
- "account_id" => $this->account_id
- ]);
- $id = Hajeebtok::$Database->LastInsertId();
- Logger::Info("Saving view tracker for account Id $this->account_id of IP address $this->ip, for video Id $this->video_id.");
- return $id;
- }
- /**
- * Deletes the object from the database.
- */
- public function Delete()
- {
- Hajeebtok::$Database->Query("DELETE FROM view_tracking WHERE id = :id", ["id" => $this->id]);
- }
- /**
- * Loads the object from the database.
- */
- public function Load()
- {
- if (empty($this->ip)) throw new ViewTrackerNotFoundException(0, 404);
- $data = Hajeebtok::$Database->Row("SELECT * FROM view_tracking WHERE ip = :ip", ["ip" => $this->ip]);
- if (empty($data)) throw new ViewTrackerNotFoundException($this->ip, 404);
- $this->id = $data["id"];
- $this->video_id = $data["video_id"];
- $this->tracking_start = strtotime($data["tracking_start"]);
- $this->ip = $data["ip"];
- $this->account_id = $data["account_id"];
- }
- public function LoadMany() {
- // not implemented
- }
- public function Exists(): bool
- {
- if (empty($this->ip)) throw new ViewTrackerNotFoundException(0, 404);
- $data = Hajeebtok::$Database->Row("SELECT * FROM view_tracking WHERE ip = :ip", ["ip" => $this->ip]);
- return !empty($data);
- }
- }
|