src/Domain/Whater/Model/WhaterDevice.php line 23

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Whater\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Ramsey\Uuid\Uuid;
  8. use Whater\Domain\Common\Exception\InvalidUUIDException;
  9. use Whater\Domain\User\Model\User;
  10. use Whater\Domain\Whater\Exception\InvalidWhaterDeviceException;
  11. use Whater\Domain\Zones\Model\Ubication;
  12. use Whater\Domain\Zones\Model\Town;
  13. use Whater\Domain\Zones\Model\Establishment;
  14. use Whater\Domain\Whater\Model\WhaterPoint;
  15. /**
  16. * Class WhaterDevice
  17. *
  18. * @package Whater\Domain\Whater\Model
  19. */
  20. class WhaterDevice implements ContainsRecordedEvents
  21. {
  22. use EventRecorderCapabilities;
  23. /**
  24. * @var string
  25. */
  26. private $uuid;
  27. /**
  28. * @var string
  29. */
  30. private $name;
  31. /**
  32. * @var string
  33. */
  34. private $description;
  35. /**
  36. * @var Ubication
  37. */
  38. private $ubication;
  39. /**
  40. * @var Establishment
  41. */
  42. private $establishment;
  43. /**
  44. * @var Collection
  45. */
  46. private $whaterPoints;
  47. /**
  48. * @var \DateTime
  49. */
  50. private $createdAt;
  51. /**
  52. * @var \DateTime
  53. */
  54. private $updatedAt;
  55. public function __construct(
  56. string $whaterDeviceId = null,
  57. string $name,
  58. string $description = null,
  59. Ubication $ubication = null,
  60. Establishment $establishment = null
  61. ) {
  62. try {
  63. $this->uuid = Uuid::fromString($whaterDeviceId ?: Uuid::uuid4())->toString();
  64. } catch (\InvalidArgumentException $e) {
  65. throw new InvalidUUIDException();
  66. }
  67. $this->name = $name;
  68. $this->description = $description;
  69. $this->establishment = $establishment;
  70. if ($establishment == null && $ubication == null) {
  71. throw new InvalidWhaterDeviceException();
  72. } else if ($establishment != null && $ubication != null && !$establishment->ubication()->equals($ubication)) {
  73. throw new InvalidWhaterDeviceException();
  74. } else if ($establishment != null) {
  75. $this->ubication = $establishment->ubication();
  76. } else {
  77. $this->ubication = $ubication;
  78. }
  79. $this->whaterPoints = new ArrayCollection();
  80. $this->createdAt = new \DateTime();
  81. $this->updatedAt = new \DateTime();
  82. }
  83. public function updateWhaterDevice(
  84. string $name,
  85. string $description = null
  86. ) {
  87. $this->name = $name;
  88. $this->description = $description;
  89. $this->updatedAt = new \DateTime();
  90. return $this;
  91. }
  92. /**
  93. * @return string
  94. */
  95. public function id(): string
  96. {
  97. return $this->uuid;
  98. }
  99. /**
  100. * @return string
  101. */
  102. public function name(): string
  103. {
  104. return $this->name;
  105. }
  106. /**
  107. * @return string
  108. */
  109. public function description(): ?string
  110. {
  111. return $this->description;
  112. }
  113. /**
  114. * @return ?Ubication
  115. */
  116. public function ubication(): ?Ubication
  117. {
  118. return $this->ubication;
  119. }
  120. /**
  121. * @return ?Establishment
  122. */
  123. public function establishment(): ?Establishment
  124. {
  125. return $this->establishment;
  126. }
  127. /**
  128. * @return Collection
  129. */
  130. public function whaterPoints(): Collection
  131. {
  132. return $this->whaterPoints;
  133. }
  134. /**
  135. * @return \DateTime
  136. */
  137. public function createdAt(): \DateTime
  138. {
  139. return $this->createdAt;
  140. }
  141. /**
  142. * @return \DateTime
  143. */
  144. public function updatedAt(): \DateTime
  145. {
  146. return $this->updatedAt;
  147. }
  148. }