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