<?php
namespace Whater\Domain\Notification\Model;
use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
use Ramsey\Uuid\Uuid;
use Whater\Domain\Common\Exception\InvalidUUIDException;
use Whater\Domain\Notification\Event\NotificationWasCreated;
use Whater\Domain\User\Model\User;
/**
*
* @package Whater\Domain\Notification\Model
*/
class Notification implements ContainsRecordedEvents
{
use EventRecorderCapabilities;
/**
* @var string
*/
private $uuid;
/**
* @var User
*/
private $user;
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $message;
/**
* @var string
*/
private $link;
/**
* @var string
*/
private $checksum;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @var \DateTime
*/
private $readAt;
public function __construct(
string $notificationId = null,
User $user = null,
string $message,
string $title = null,
string $link = null
) {
try {
$this->uuid = Uuid::fromString($notificationId ?: Uuid::uuid4())->toString();
} catch (\InvalidArgumentException $e) {
throw new InvalidUUIDException();
}
$this->user = $user;
$this->message = $message;
$this->title = $title;
$this->link = $link;
$this->createdAt = new \DateTime();
//Calculate checksum
$notificationDate = clone $this->createdAt();
$notificationDate->setTimezone(new \DateTimeZone('UTC'));
$hash = md5($notificationDate->format('dmYHis'));
$hash = md5($hash . $this->title());
$hash = md5($hash . $this->user());
$this->checksum = $hash;
$this->record(new NotificationWasCreated($this));
}
public function readNotification(): Notification
{
if ($this->readAt() == null) {
$this->readAt = new \DateTime();
}
return $this;
}
public function unreadNotification(): Notification
{
$this->readAt = null;
return $this;
}
/**
* @return string
*/
public function id(): string
{
return $this->uuid;
}
/**
* @return string
*/
public function message(): string
{
return $this->message;
}
/**
* @return string|null
*/
public function title(): ?string
{
return $this->title;
}
/**
* @return string|null
*/
public function link(): ?string
{
return $this->link;
}
/**
* @return string|null
*/
public function checksum(): ?string
{
return $this->checksum;
}
/**
* @return User
*/
public function user(): User
{
return $this->user;
}
/**
* @return \DateTime
*/
public function createdAt(): \DateTime
{
return $this->createdAt;
}
/**
* @return \DateTime|null
*/
public function readAt(): ?\DateTime
{
return $this->readAt;
}
}