<?php
namespace Sq\Entity\Schema\ORM;
use Doctrine\ORM\Mapping as ORM;
use Sq\Entity\Enum;
use Sq\Entity\Schema\DocumentObject;
#[ORM\Entity(repositoryClass: \Sq\Service\Repository\ORM\NotificationRepository::class)]
#[ORM\Table(name: 'notifications')]
class Notification
{
/**
* @var int
*/
#[ORM\Column(name: 'n_id', type: 'integer', nullable: false, options: ['unsigned' => true])]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
protected $id;
/**
* @var string
*/
#[ORM\Column(name: 'n_name', type: 'string', length: 255, nullable: false)]
protected $name;
/**
* @var string
*/
#[ORM\Column(name: 'n_message', type: 'string', length: 1000, nullable: false)]
protected $message;
/**
* @var string
*/
#[ORM\Column(name: 'n_datetime', type: 'datetime', nullable: false)]
protected $dateTime;
/**
* @var string
*/
#[ORM\Column(name: 'n_scope', type: 'string', length: 255, nullable: false)]
protected $scope;
/**
* @var int
*/
#[ORM\Column(name: 'n_read', type: 'boolean', nullable: false)]
protected $isRead = false;
/**
* @var User
*/
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(name: 'n_user_id', referencedColumnName: 'u_id', nullable: false)]
protected $user;
/**
* @var int|null
*/
#[ORM\ManyToOne(targetEntity: Organization::class)]
#[ORM\JoinColumn(name: 'n_org_id', referencedColumnName: 'o_id', nullable: true)]
private $organization;
/**
* @var int|null
*/
#[ORM\ManyToOne(targetEntity: Workspace::class)]
#[ORM\JoinColumn(name: 'n_workspace_id', referencedColumnName: 'ws_id', nullable: true)]
private $workspace;
/**
* @var array|null
*/
#[ORM\Column(name: 'n_metadata', type: 'json', nullable: true)]
private $metadata;
public function __construct(
Enum\Notification\NotificationName $name,
Enum\Notification\NotificationScope $scope,
string $message,
User $user,
?Workspace $workspace = null,
?Organization $organization = null
) {
$this->name = $name->getValue();
$this->scope = $scope->getValue();
$this->message = $message;
$this->user = $user;
$this->organization = $organization;
$this->workspace = $workspace;
$this->dateTime = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): Enum\Notification\NotificationName
{
return Enum\Notification\NotificationName::from($this->name);
}
public function getScope(): Enum\Notification\NotificationScope
{
return Enum\Notification\NotificationScope::from($this->scope);
}
public function getMessage(): string
{
return $this->message;
}
public function isRead(): bool
{
return $this->isRead;
}
public function markAsRead(): self
{
$this->isRead = true;
return $this;
}
public function getDateTime(): \DateTimeInterface
{
return $this->dateTime;
}
public function getUser(): User
{
return $this->user;
}
public function getOrganization(): ?Organization
{
return $this->organization;
}
public function getWorkspace(): ?Workspace
{
return $this->workspace;
}
public function getMetadata(): ?DocumentObject\Notification\Metadata
{
return DocumentObject\Notification\Metadata::createFromTypeAndData(
$this->getName(),
$this->metadata
);
}
public function setMetadata(?DocumentObject\Notification\Metadata $metadata): self
{
$this->metadata = $metadata?->serialize();
return $this;
}
}