<?php
namespace Whater\Domain\Zones\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\Zones\Model\Town;
use Whater\Domain\Whater\Model\DistributionNetwork;
use Whater\Domain\Whater\Model\WhaterOrganization;
/**
* Class Ubication
*
* @package Whater\Domain\Zones\Model
*/
class Ubication implements ContainsRecordedEvents
{
use EventRecorderCapabilities;
/**
* @var string
*/
private $uuid;
/**
* @var string
*/
private $slug;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $postalCode;
/**
* @var string
*/
private $email;
/**
* @var float
*/
private $latitude;
/**
* @var float
*/
private $longitude;
/**
* @var int
*/
private $population;
/**
* @var Town
*/
private $town;
/**
* @var TownLocation
*/
private $townLocation;
/**
* @var DistributionNetwork
*/
private $distributionNetwork;
/**
* @var WhaterOrganization
*/
private $whaterOrganization;
/**
* @var Collection
*/
private $valorations;
/**
* @var Collection
*/
private $whaterPoints;
/**
* @var Collection
*/
private $establishments;
/**
* @var Collection
*/
private $images;
/**
* @var Collection
*/
private $comments;
/**
* @var Collection
*/
private $ownershipRequests;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @var \DateTime
*/
private $updatedAt;
/**
* @var Collection
*/
private $whaterDevices;
public function __construct(
string $locationId = null,
Town $town,
DistributionNetwork $distributionNetwork = null,
string $name,
string $slug = null,
string $postalCode = null,
string $latitude = null,
string $longitude = null,
TownLocation $townLocation = null
) {
try {
$this->uuid = Uuid::fromString($locationId ?: Uuid::uuid4())->toString();
} catch (\InvalidArgumentException $e) {
throw new InvalidUUIDException();
}
$this->slug = $slug;
$this->name = $name;
$this->town = $town;
$this->townLocation = $townLocation;
$this->postalCode = $postalCode;
$this->whaterPoints = new ArrayCollection();
$this->establishments = new ArrayCollection();
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
$this->latitude = $latitude;
$this->longitude = $longitude;
if ($this->latitude == null || $this->longitude == null) {
$this->latitude = $town->latitude();
$this->longitude = $town->longitude();
}
$this->distributionNetwork = $distributionNetwork;
$this->valorations = new ArrayCollection();
$this->images = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->whaterDevices = new ArrayCollection();
$this->ownershipRequests = new ArrayCollection();
}
public function updateUbicationData(
string $name,
string $slug = null,
float $latitude = null,
float $longitude = null,
string $postalCode = null,
DistributionNetwork $distributionNetwork = null,
string $email = null,
int $population = null,
TownLocation $townLocation = null
) {
$this->name = $name;
$this->slug = $slug;
if ($latitude != null && $longitude != null) {
$this->latitude = $latitude;
$this->longitude = $longitude;
}
$this->postalCode = $postalCode;
$this->distributionNetwork = $distributionNetwork;
$this->email = $email;
$this->population = $population;
if($townLocation != null){
$this->townLocation = $townLocation;
}
$this->updatedAt = new \DateTime();
}
public function addWhaterOrganization(
WhaterOrganization $whaterOrganization
)
{
$this->whaterOrganization = $whaterOrganization;
$this->updatedAt = new \DateTime();
}
/**
* @return string
*/
public function id(): string
{
return $this->uuid;
}
/**
* @return string
*/
public function name(): string
{
return $this->name;
}
/**
* @return string
*/
public function slug(): ?string
{
return $this->slug;
}
/**
* @return string
*/
public function postalCode(): ?string
{
return $this->postalCode;
}
/**
* @return string
*/
public function email(): ?string
{
return $this->email;
}
/**
* @return float|null
*/
public function latitude(): ?float
{
return $this->latitude;
}
/**
* @return float|null
*/
public function longitude(): ?float
{
return $this->longitude;
}
/**
* @return integer|null
*/
public function population(): ?int
{
return $this->population;
}
/**
* @return Town
*/
public function town(): Town
{
return $this->town;
}
/**
* @return TownLocation|null
*/
public function townLocation(): ?TownLocation
{
return $this->townLocation;
}
/**
* @return DistributionNetwork
*/
public function distributionNetwork(): ?DistributionNetwork
{
return $this->distributionNetwork;
}
/**
* @return Collection
*/
public function valorations(): Collection
{
return $this->valorations;
}
/**
* @return Collection
*/
public function images(): Collection
{
return $this->images;
}
/**
* @return Collection
*/
public function comments(): Collection
{
return $this->comments;
}
/**
* @return Collection
*/
public function ownershipRequests(): Collection
{
return $this->ownershipRequests;
}
/**
* @return Collection
*/
public function whaterPoints(): Collection
{
return $this->whaterPoints;
}
/**
* @return Collection
*/
public function whaterDevices(): Collection
{
return $this->whaterDevices;
}
/**
* @return Collection
*/
public function establishments(): Collection
{
return $this->establishments;
}
/**
* @return \DateTime
*/
public function createdAt(): \DateTime
{
return $this->createdAt;
}
/**
* @return \DateTime
*/
public function updatedAt(): \DateTime
{
return $this->updatedAt;
}
/**
* @return WhaterOrganization
*/
public function whaterOrganization(): ?WhaterOrganization
{
return $this->whaterOrganization;
}
/**
* @return bool
*/
public function equals(Ubication $ubication)
{
return $this->id() === $ubication->id();
}
}