appwrite/src/Appwrite/Event/Func.php

244 lines
5 KiB
PHP
Raw Normal View History

2022-04-17 20:34:32 +00:00
<?php
namespace Appwrite\Event;
use Utopia\Database\Document;
2022-11-15 18:13:17 +00:00
use Utopia\Queue\Client;
use Utopia\Queue\Connection;
2022-04-17 20:34:32 +00:00
class Func extends Event
{
2024-10-14 13:06:08 +00:00
public const TYPE_ASYNC_WRITE = 'async_write';
2022-04-17 20:34:32 +00:00
protected string $jwt = '';
protected string $type = '';
2023-02-14 11:01:38 +00:00
protected string $body = '';
protected string $path = '';
protected string $method = '';
protected array $headers = [];
2024-06-17 12:44:12 +00:00
protected ?string $functionId = null;
2022-04-18 16:21:45 +00:00
protected ?Document $function = null;
protected ?Document $execution = null;
2022-04-17 20:34:32 +00:00
2022-11-15 18:13:17 +00:00
public function __construct(protected Connection $connection)
2022-04-17 20:34:32 +00:00
{
2023-06-02 03:54:34 +00:00
parent::__construct($connection);
$this
->setQueue(Event::FUNCTIONS_QUEUE_NAME)
->setClass(Event::FUNCTIONS_CLASS_NAME);
2022-04-17 20:34:32 +00:00
}
2022-04-18 16:21:45 +00:00
/**
* Sets function document for the function event.
*
2022-05-10 12:31:20 +00:00
* @param Document $function
2022-04-18 16:21:45 +00:00
* @return self
*/
2022-04-17 20:34:32 +00:00
public function setFunction(Document $function): self
{
$this->function = $function;
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns set function document for the function event.
*
2022-05-23 14:54:50 +00:00
* @return null|Document
2022-04-18 16:21:45 +00:00
*/
2022-04-17 20:34:32 +00:00
public function getFunction(): ?Document
{
return $this->function;
}
2024-06-17 12:44:12 +00:00
/**
* Sets function id for the function event.
2024-06-17 12:46:18 +00:00
*
2024-06-17 12:44:12 +00:00
* @param string $functionId
*/
public function setFunctionId(string $functionId): self
{
$this->functionId = $functionId;
return $this;
}
/**
* Returns set function id for the function event.
2024-06-17 12:46:18 +00:00
*
2024-06-17 12:44:12 +00:00
* @return string|null
*/
public function getFunctionId(): ?string
{
return $this->functionId;
}
2022-04-18 16:21:45 +00:00
/**
* Sets execution for the function event.
*
2022-05-10 12:31:20 +00:00
* @param Document $execution
2022-04-18 16:21:45 +00:00
* @return self
*/
2022-04-17 20:34:32 +00:00
public function setExecution(Document $execution): self
{
$this->execution = $execution;
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns set execution for the function event.
*
2022-05-10 12:31:20 +00:00
* @return null|Document
2022-04-18 16:21:45 +00:00
*/
2022-04-17 20:34:32 +00:00
public function getExecution(): ?Document
{
return $this->execution;
}
2022-04-18 16:21:45 +00:00
/**
* Sets type for the function event.
*
* @param string $type Can be `schedule`, `event` or `http`.
* @return self
*/
2022-04-17 20:34:32 +00:00
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns set type for the function event.
*
* @return string
*/
2022-04-17 20:34:32 +00:00
public function getType(): string
{
return $this->type;
}
2022-04-18 16:21:45 +00:00
/**
2023-02-14 11:01:38 +00:00
* Sets custom body for the function event.
2022-04-18 16:21:45 +00:00
*
2023-02-14 11:01:38 +00:00
* @param string $body
2022-04-18 16:21:45 +00:00
* @return self
*/
2023-02-14 11:01:38 +00:00
public function setBody(string $body): self
2022-04-17 20:34:32 +00:00
{
2023-02-14 11:01:38 +00:00
$this->body = $body;
return $this;
}
/**
* Sets custom method for the function event.
*
* @param string $method
* @return self
*/
public function setMethod(string $method): self
{
$this->method = $method;
return $this;
}
/**
* Sets custom path for the function event.
*
* @param string $path
* @return self
*/
public function setPath(string $path): self
{
$this->path = $path;
return $this;
}
/**
* Sets custom headers for the function event.
*
* @param string $headers
* @return self
*/
public function setHeaders(array $headers): self
{
$this->headers = $headers;
2022-04-17 20:34:32 +00:00
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns set custom data for the function event.
*
* @return string
*/
2024-10-08 07:54:40 +00:00
public function getData(): string
2022-04-17 20:34:32 +00:00
{
2024-10-08 07:54:40 +00:00
return $this->data;
2022-04-17 20:34:32 +00:00
}
2022-04-18 16:21:45 +00:00
/**
* Sets JWT for the function event.
*
* @param string $jwt
* @return self
*/
2022-04-17 20:34:32 +00:00
public function setJWT(string $jwt): self
{
$this->jwt = $jwt;
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns set JWT for the function event.
*
* @return string
*/
2022-04-17 20:34:32 +00:00
public function getJWT(): string
{
return $this->jwt;
}
2022-04-18 16:21:45 +00:00
/**
* Executes the function event and sends it to the functions worker.
*
* @return string|bool
* @throws \InvalidArgumentException
*/
2022-04-17 20:34:32 +00:00
public function trigger(): string|bool
{
2024-11-08 20:55:53 +00:00
if ($this->paused) {
return false;
}
2025-01-16 07:44:47 +00:00
$this->trimFields();
2022-11-16 05:30:57 +00:00
$client = new Client($this->queue, $this->connection);
2022-11-15 18:13:17 +00:00
2022-11-16 05:52:19 +00:00
$events = $this->getEvent() ? Event::generateEvents($this->getEvent(), $this->getParams()) : null;
2022-11-16 05:30:57 +00:00
return $client->enqueue([
'project' => $this->project,
'user' => $this->user,
2024-09-07 10:20:23 +00:00
'userId' => $this->userId,
2022-11-15 18:13:17 +00:00
'function' => $this->function,
2024-06-17 12:44:12 +00:00
'functionId' => $this->functionId,
2022-11-16 05:30:57 +00:00
'execution' => $this->execution,
'type' => $this->type,
2022-04-17 20:34:32 +00:00
'jwt' => $this->jwt,
2022-11-16 13:50:12 +00:00
'payload' => $this->payload,
2022-11-16 05:52:19 +00:00
'events' => $events,
2023-02-14 11:01:38 +00:00
'body' => $this->body,
'path' => $this->path,
'headers' => $this->headers,
'method' => $this->method,
2022-04-17 20:34:32 +00:00
]);
}
2022-04-18 16:21:45 +00:00
}