src/Domain/Zones/Model/UbicationValoration.php line 18

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Zones\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. /**
  11. * Class UbicationValoration
  12. *
  13. * @package Whater\Domain\Whater\Model
  14. */
  15. class UbicationValoration implements ContainsRecordedEvents
  16. {
  17. use EventRecorderCapabilities;
  18. public const WP_MAX_VALORATION = 5;
  19. public const WP_MIN_VALORATION = 0;
  20. /**
  21. * @var string
  22. */
  23. private $uuid;
  24. /**
  25. * @var Ubication
  26. */
  27. private $ubication;
  28. /**
  29. * @var User
  30. */
  31. private $createdBy;
  32. /**
  33. * @var \DateTime
  34. */
  35. private $createdAt;
  36. /**
  37. * @var int
  38. */
  39. private $parameter1;
  40. /**
  41. * @var int
  42. */
  43. private $parameter2;
  44. /**
  45. * @var int
  46. */
  47. private $parameter3;
  48. /**
  49. * @var int
  50. */
  51. private $parameter4;
  52. /**
  53. * @var bool
  54. */
  55. private $isProfessionalValoration;
  56. public function __construct(
  57. string $ubicationValorationId = null,
  58. Ubication $ubication,
  59. User $createdBy,
  60. int $parameter1 = null,
  61. int $parameter2 = null,
  62. int $parameter3 = null,
  63. int $parameter4 = null,
  64. bool $isProfessionalValoration = false
  65. ) {
  66. try {
  67. $this->uuid = Uuid::fromString($ubicationValorationId ?: Uuid::uuid4())->toString();
  68. } catch (\InvalidArgumentException $e) {
  69. throw new InvalidUUIDException();
  70. }
  71. $this->ubication = $ubication;
  72. $this->createdBy = $createdBy;
  73. $this->isProfessionalValoration = $isProfessionalValoration;
  74. $this->updateValoration(
  75. $parameter1,
  76. $parameter2,
  77. $parameter3,
  78. $parameter4
  79. );
  80. $this->createdAt = new \DateTime();
  81. }
  82. public function updateValoration(
  83. int $parameter1 = null,
  84. int $parameter2 = null,
  85. int $parameter3 = null,
  86. int $parameter4 = null
  87. ): UbicationValoration {
  88. if (!$this->ubication()->valorations()->contains($this)) {
  89. $this->ubication()->valorations()->add($this);
  90. }
  91. if (!is_null($parameter1)) {
  92. $this->parameter1 = $parameter1;
  93. if ($this->parameter1 < UbicationValoration::WP_MIN_VALORATION) {
  94. $this->parameter1 = UbicationValoration::WP_MIN_VALORATION;
  95. } else if ($this->parameter1 > UbicationValoration::WP_MAX_VALORATION) {
  96. $this->parameter1 = UbicationValoration::WP_MAX_VALORATION;
  97. }
  98. }
  99. if (!is_null($parameter2)) {
  100. $this->parameter2 = $parameter2;
  101. if ($this->parameter2 < UbicationValoration::WP_MIN_VALORATION) {
  102. $this->parameter2 = UbicationValoration::WP_MIN_VALORATION;
  103. } else if ($this->parameter2 > UbicationValoration::WP_MAX_VALORATION) {
  104. $this->parameter2 = UbicationValoration::WP_MAX_VALORATION;
  105. }
  106. }
  107. if (!is_null($parameter3)) {
  108. $this->parameter3 = $parameter3;
  109. if ($this->parameter3 < UbicationValoration::WP_MIN_VALORATION) {
  110. $this->parameter3 = UbicationValoration::WP_MIN_VALORATION;
  111. } else if ($this->parameter3 > UbicationValoration::WP_MAX_VALORATION) {
  112. $this->parameter3 = UbicationValoration::WP_MAX_VALORATION;
  113. }
  114. }
  115. if (!is_null($parameter4)) {
  116. $this->parameter4 = $parameter4;
  117. if ($this->parameter4 < UbicationValoration::WP_MIN_VALORATION) {
  118. $this->parameter4 = UbicationValoration::WP_MIN_VALORATION;
  119. } else if ($this->parameter4 > UbicationValoration::WP_MAX_VALORATION) {
  120. $this->parameter4 = UbicationValoration::WP_MAX_VALORATION;
  121. }
  122. }
  123. $this->ubication()->updateValorations();
  124. return $this;
  125. }
  126. /**
  127. * @return string
  128. */
  129. public function id(): string
  130. {
  131. return $this->uuid;
  132. }
  133. /**
  134. * @return Ubication
  135. */
  136. public function ubication(): Ubication
  137. {
  138. return $this->ubication;
  139. }
  140. /**
  141. * @return User
  142. */
  143. public function createdBy(): User
  144. {
  145. return $this->createdBy;
  146. }
  147. /**
  148. * @return int
  149. */
  150. public function parameter1(): ?int
  151. {
  152. return $this->parameter1;
  153. }
  154. /**
  155. * @return int
  156. */
  157. public function parameter2(): ?int
  158. {
  159. return $this->parameter2;
  160. }
  161. /**
  162. * @return int
  163. */
  164. public function parameter3(): ?int
  165. {
  166. return $this->parameter3;
  167. }
  168. /**
  169. * @return int
  170. */
  171. public function parameter4(): ?int
  172. {
  173. return $this->parameter4;
  174. }
  175. /**
  176. * @return bool
  177. */
  178. public function isProfessionalValoration(): bool
  179. {
  180. return $this->isProfessionalValoration;
  181. }
  182. /**
  183. * @return \DateTime
  184. */
  185. public function createdAt(): \DateTime
  186. {
  187. return $this->createdAt;
  188. }
  189. /**
  190. * @return bool
  191. */
  192. public function equals(UbicationValoration $ubicationValoration)
  193. {
  194. return $this->id() === $ubicationValoration->id();
  195. }
  196. }