appwrite/src/Appwrite/Event/Database.php
2025-04-27 10:33:44 +05:30

132 lines
2.8 KiB
PHP

<?php
namespace Appwrite\Event;
use Utopia\Database\Document;
use Utopia\DSN\DSN;
use Utopia\Queue\Publisher;
class Database extends Event
{
protected string $type = '';
protected ?Document $database = null;
protected ?Document $table = null;
protected ?Document $row = null;
public function __construct(protected Publisher $publisher)
{
parent::__construct($publisher);
$this->setClass(Event::DATABASE_CLASS_NAME);
}
/**
* Sets the type for this database event (use the constants starting with DATABASE_TYPE_*).
*
* @param string $type
* @return self
*/
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
/**
* Returns the set type for the database event.
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* Set the database for this event
*
* @param Document $database
* @return self
*/
public function setDatabase(Document $database): self
{
$this->database = $database;
return $this;
}
/**
* Set the table for this database event.
*
* @param Document $table
* @return self
*/
public function setTable(Document $table): self
{
$this->table = $table;
return $this;
}
/**
* Returns set table for this event.
*
* @return null|Document
*/
public function getTable(): ?Document
{
return $this->table;
}
/**
* Set the row for this database event.
*
* @param Document $row
* @return self
*/
public function setRow(Document $row): self
{
$this->row = $row;
return $this;
}
/**
* Returns set row for this database event.
* @return null|Document
*/
public function getRow(): ?Document
{
return $this->row;
}
public function getQueue(): string
{
try {
$dsn = new DSN($this->getProject()->getAttribute('database'));
} catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $this->getProject()->getAttribute('database'));
}
$this->queue = $dsn->getHost();
return $this->queue;
}
/**
* Prepare the payload for the event
*
* @return array
*/
protected function preparePayload(): array
{
return [
'project' => $this->project,
'user' => $this->user,
'type' => $this->type,
'table' => $this->table,
'row' => $this->row,
'database' => $this->database,
'events' => Event::generateEvents($this->getEvent(), $this->getParams())
];
}
}