<?php
namespace Whater\Domain\Whater\Model;
use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Ramsey\Uuid\Uuid;
use Whater\Domain\Common\Exception\InvalidUUIDException;
use Whater\Domain\User\Model\User;
use Whater\Domain\Whater\Exception\InvalidWhaterDeviceException;
use Whater\Domain\Zones\Model\Ubication;
use Whater\Domain\Zones\Model\Town;
use Whater\Domain\Zones\Model\Establishment;
use Whater\Domain\Whater\Model\WhaterPoint;
/**
* Class WhaterDevice
*
* @package Whater\Domain\Whater\Model
*/
class WhaterDevice implements ContainsRecordedEvents
{
use EventRecorderCapabilities;
/**
* @var string
*/
private $uuid;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $description;
/**
* @var Ubication
*/
private $ubication;
/**
* @var Establishment
*/
private $establishment;
/**
* @var Collection
*/
private $whaterPoints;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @var \DateTime
*/
private $updatedAt;
public function __construct(
string $whaterDeviceId = null,
string $name,
string $description = null,
Ubication $ubication = null,
Establishment $establishment = null
) {
try {
$this->uuid = Uuid::fromString($whaterDeviceId ?: Uuid::uuid4())->toString();
} catch (\InvalidArgumentException $e) {
throw new InvalidUUIDException();
}
$this->name = $name;
$this->description = $description;
$this->establishment = $establishment;
if ($establishment == null && $ubication == null) {
throw new InvalidWhaterDeviceException();
} else if ($establishment != null && $ubication != null && !$establishment->ubication()->equals($ubication)) {
throw new InvalidWhaterDeviceException();
} else if ($establishment != null) {
$this->ubication = $establishment->ubication();
} else {
$this->ubication = $ubication;
}
$this->whaterPoints = new ArrayCollection();
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
public function updateWhaterDevice(
string $name,
string $description = null
) {
$this->name = $name;
$this->description = $description;
$this->updatedAt = new \DateTime();
return $this;
}
/**
* @return string
*/
public function id(): string
{
return $this->uuid;
}
/**
* @return string
*/
public function name(): string
{
return $this->name;
}
/**
* @return string
*/
public function description(): ?string
{
return $this->description;
}
/**
* @return ?Ubication
*/
public function ubication(): ?Ubication
{
return $this->ubication;
}
/**
* @return ?Establishment
*/
public function establishment(): ?Establishment
{
return $this->establishment;
}
/**
* @return Collection
*/
public function whaterPoints(): Collection
{
return $this->whaterPoints;
}
/**
* @return \DateTime
*/
public function createdAt(): \DateTime
{
return $this->createdAt;
}
/**
* @return \DateTime
*/
public function updatedAt(): \DateTime
{
return $this->updatedAt;
}
}