src/Domain/Whater/Model/ImportDataFile.php line 18

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 Ramsey\Uuid\Uuid;
  6. use Whater\Domain\Common\Exception\InvalidUUIDException;
  7. use Whater\Domain\User\Model\User;
  8. use Whater\Domain\Whater\Exception\InvalidImportDataFileStatusException;
  9. use Whater\Domain\Whater\Exception\InvalidImportDataFileTypeException;
  10. /**
  11. * Class ImportDataFile
  12. *
  13. * @package Whater\Domain\Whater\Model
  14. */
  15. class ImportDataFile implements ContainsRecordedEvents
  16. {
  17. use EventRecorderCapabilities;
  18. const STATUS_PENDING = 'STATUS_PENDING';
  19. const STATUS_PROCESING = 'STATUS_PROCESING';
  20. const STATUS_FINISHED = 'STATUS_FINISHED';
  21. const STATUS_CANCELED = 'STATUS_CANCELED';
  22. const STATUS_FAIL = 'STATUS_FAIL';
  23. const FILE_TYPE_WHATERPOINTS_XLS = 'FT_WHATERPOINTS_XLS';
  24. const FILE_TYPE_DISTRIBUTION_NETWORKS_XLSLS = 'FT_DISTRIBUTION_NETWORKS_XLS';
  25. const FILE_TYPE_ESTABLISHMENTS_XLS = 'FT_ESTABLISHMENTS_XLS';
  26. const FILE_TYPE_UBICATIONS_XLS = 'FT_UBICATIONS_XLS';
  27. const FILE_TYPE_TOWN_LOCATIONS_XLS = 'FT_TOWN_LOCATIONS_XLS';
  28. const FILE_TYPE_TOWNS_XLS = 'FT_TOWNS_XLS';
  29. const FILE_TYPE_AGRUPATIONS_XLS = 'FT_AGRUPATIONS_XLS';
  30. const FILE_TYPE_COUNTRY_AREAS_XLS = 'FT_COUNTRY_AREAS_XLS';
  31. const FILE_TYPE_COUNTRIES_XLS = 'FT_COUNTRIES_XLS';
  32. const FILE_TYPE_WHATER_DEVICES_XLS = 'FT_WHATER_DEVICES_XLS';
  33. const FILE_TYPE_USERS_XLS = 'FT_USERS_XLS';
  34. /**
  35. * @var string
  36. */
  37. private $uuid;
  38. /**
  39. * @var string
  40. */
  41. private $fileType;
  42. /**
  43. * @var string
  44. */
  45. private $filePath;
  46. /**
  47. * @var string
  48. */
  49. private $fileName;
  50. /**
  51. * @var string
  52. */
  53. private $status;
  54. /**
  55. * @var int
  56. */
  57. private $numRows;
  58. /**
  59. * @var int
  60. */
  61. private $numSuccessRows;
  62. /**
  63. * @var int
  64. */
  65. private $numInvalidRows;
  66. /**
  67. * @var \DateTime
  68. */
  69. private $createdAt;
  70. /**
  71. * @var \DateTime
  72. */
  73. private $updatedAt;
  74. /**
  75. * @var User
  76. */
  77. private $uploadedBy;
  78. /**
  79. * @var array
  80. */
  81. private $notesLines;
  82. public function __construct(
  83. string $importDataFileId = null,
  84. string $fileType,
  85. string $filePath,
  86. string $fileName,
  87. int $numRows,
  88. User $uploadedBy
  89. ) {
  90. try {
  91. $this->uuid = Uuid::fromString($importDataFileId ?: Uuid::uuid4())->toString();
  92. } catch (\InvalidArgumentException $e) {
  93. throw new InvalidUUIDException();
  94. }
  95. switch ($fileType) {
  96. case ImportDataFile::FILE_TYPE_WHATERPOINTS_XLS;
  97. case ImportDataFile::FILE_TYPE_DISTRIBUTION_NETWORKS_XLSLS;
  98. $this->fileType = $fileType;
  99. break;
  100. case ImportDataFile::FILE_TYPE_UBICATIONS_XLS;
  101. $this->fileType = $fileType;
  102. break;
  103. case ImportDataFile::FILE_TYPE_ESTABLISHMENTS_XLS;
  104. $this->fileType = $fileType;
  105. break;
  106. case ImportDataFile::FILE_TYPE_TOWN_LOCATIONS_XLS;
  107. $this->fileType = $fileType;
  108. break;
  109. case ImportDataFile::FILE_TYPE_TOWNS_XLS;
  110. $this->fileType = $fileType;
  111. break;
  112. case ImportDataFile::FILE_TYPE_AGRUPATIONS_XLS;
  113. $this->fileType = $fileType;
  114. break;
  115. case ImportDataFile::FILE_TYPE_COUNTRY_AREAS_XLS;
  116. $this->fileType = $fileType;
  117. break;
  118. case ImportDataFile::FILE_TYPE_COUNTRIES_XLS;
  119. $this->fileType = $fileType;
  120. break;
  121. case ImportDataFile::FILE_TYPE_WHATER_DEVICES_XLS;
  122. $this->fileType = $fileType;
  123. break;
  124. case ImportDataFile::FILE_TYPE_USERS_XLS;
  125. $this->fileType = $fileType;
  126. break;
  127. default:
  128. throw new InvalidImportDataFileTypeException();
  129. }
  130. $this->uploadedBy = $uploadedBy;
  131. $this->filePath = $filePath;
  132. $this->fileName = $fileName;
  133. $this->status = ImportDataFile::STATUS_PENDING;
  134. $this->numRows = $numRows;
  135. $this->numSuccessRows = 0;
  136. $this->numInvalidRows = 0;
  137. $this->notesLines = [];
  138. $this->createdAt = new \DateTime();
  139. $this->updatedAt = new \DateTime();
  140. }
  141. public function updateStatus(
  142. string $status,
  143. int $numSuccessRows,
  144. int $numInvalidRows,
  145. array $notesLines
  146. ) {
  147. switch ($status) {
  148. case ImportDataFile::STATUS_PENDING:
  149. case ImportDataFile::STATUS_CANCELED:
  150. case ImportDataFile::STATUS_PROCESING:
  151. case ImportDataFile::STATUS_FINISHED:
  152. case ImportDataFile::STATUS_FAIL:
  153. $this->status = $status;
  154. break;
  155. default:
  156. throw new InvalidImportDataFileStatusException();
  157. }
  158. $this->numSuccessRows = $numSuccessRows;
  159. $this->numInvalidRows = $numInvalidRows;
  160. $this->notesLines = $notesLines;
  161. $this->updatedAt = new \DateTime();
  162. }
  163. /**
  164. * @return string
  165. */
  166. public function id(): string
  167. {
  168. return $this->uuid;
  169. }
  170. /**
  171. * @return string
  172. */
  173. public function fileType(): string
  174. {
  175. return $this->fileType;
  176. }
  177. /**
  178. * @return string
  179. */
  180. public function fileName(): string
  181. {
  182. return $this->fileName;
  183. }
  184. /**
  185. * @return string
  186. */
  187. public function filePath(): string
  188. {
  189. return $this->filePath;
  190. }
  191. /**
  192. * @return string
  193. */
  194. public function status(): string
  195. {
  196. return $this->status;
  197. }
  198. /**
  199. * @return int
  200. */
  201. public function numRows(): int
  202. {
  203. return $this->numRows;
  204. }
  205. /**
  206. * @return int
  207. */
  208. public function numSuccessRows(): int
  209. {
  210. return $this->numSuccessRows;
  211. }
  212. /**
  213. * @return int
  214. */
  215. public function numInvalidRows(): int
  216. {
  217. return $this->numInvalidRows;
  218. }
  219. /**
  220. * @return int
  221. */
  222. public function numProcessedRows(): int
  223. {
  224. return $this->numInvalidRows + $this->numSuccessRows;
  225. }
  226. /**
  227. * @return int
  228. */
  229. public function uploadedBy(): User
  230. {
  231. return $this->uploadedBy;
  232. }
  233. /**
  234. * @return array
  235. */
  236. public function notesLines(): array
  237. {
  238. return $this->notesLines;
  239. }
  240. /**
  241. * @return \DateTime
  242. */
  243. public function createdAt(): \DateTime
  244. {
  245. return $this->createdAt;
  246. }
  247. /**
  248. * @return \DateTime
  249. */
  250. public function updatedAt(): \DateTime
  251. {
  252. return $this->updatedAt;
  253. }
  254. /**
  255. * @return bool
  256. */
  257. public function equals(ImportDataFile $importDataFile)
  258. {
  259. return $this->id() === $importDataFile->id();
  260. }
  261. }