appwrite/src/Appwrite/Event/Func.php

181 lines
3.8 KiB
PHP
Raw Normal View History

2022-04-17 20:34:32 +00:00
<?php
namespace Appwrite\Event;
use DateTime;
use Resque;
use ResqueScheduler;
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
{
protected string $jwt = '';
protected string $type = '';
protected string $data = '';
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
{
parent::__construct(Event::FUNCTIONS_QUEUE_NAME, Event::FUNCTIONS_CLASS_NAME);
}
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;
}
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
/**
* Sets custom data for the function event.
*
* @param string $data
* @return self
*/
2022-04-17 20:34:32 +00:00
public function setData(string $data): self
{
$this->data = $data;
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns set custom data for the function event.
*
* @return string
*/
2022-04-17 20:34:32 +00:00
public function getData(): string
{
return $this->data;
}
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
{
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:30:57 +00:00
return $client->enqueue([
'project' => $this->project,
'user' => $this->user,
2022-11-15 18:13:17 +00:00
'function' => $this->function,
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 05:30:57 +00:00
'payload' => '',
'events' => Event::generateEvents($this->getEvent(), $this->getParams()),
'data' => $this->data,
2022-04-17 20:34:32 +00:00
]);
}
2022-11-16 05:30:57 +00:00
/**
* Generate a function event from a base event
*
* @param Event $event
*
* @return self
*
*/
public function from(Event $event): self
{
$this->project = $event->getProject();
$this->user = $event->getUser();
$this->payload = $event->getPayload();
$this->event = $event->getEvent();
$this->params = $event->getParams();
return $this;
}
2022-04-18 16:21:45 +00:00
}