src/Domain/Product/Model/WalletLiquidationRequest.php line 17

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Product\Model;
  3. use Whater\Domain\Common\Exception\InvalidUUIDException;
  4. use Ramsey\Uuid\Uuid;
  5. use Whater\Domain\Product\Exception\InvalidWalletLiquidationRequestCurrencyException;
  6. use Whater\Domain\Product\Exception\InvalidWalletLiquidationRequestMoneyException;
  7. use Whater\Domain\Product\Exception\InvalidWalletLiquidationRequestStatusException;
  8. use Whater\Domain\Product\Exception\InvalidWalletLiquidationRequestTypeException;
  9. use Whater\Domain\Whater\Model\WhaterOrganization;
  10. /**
  11. *
  12. * @package Whater\Domain\Product\Model
  13. */
  14. class WalletLiquidationRequest
  15. {
  16. const WLR_TYPE_REQUEST_BY_ORGANIZATION = 'WLR_TYPE_REQUEST_BY_ORGANIZATION';
  17. const WLR_TYPE_REQUEST_BY_ADMIN = 'WLR_TYPE_REQUEST_BY_ADMIN';
  18. const WLR_TYPE_REQUEST_AUTOMATICALLY = 'WLR_TYPE_REQUEST_AUTOMATICALLY';
  19. const WLR_STATUS_PENDING = 'WLR_STATUS_PENDING';
  20. const WLR_STATUS_CONFIRMED = 'WLR_STATUS_CONFIRMED';
  21. const WLR_STATUS_CANCELED = 'WLR_STATUS_CANCELED';
  22. /**
  23. * @var string
  24. */
  25. protected $uuid;
  26. /**
  27. * @var string
  28. */
  29. protected $status;
  30. /**
  31. * @var string
  32. */
  33. protected $requestType;
  34. /**
  35. * @var \DateTime
  36. */
  37. protected $liquidationDate;
  38. /**
  39. * @var string
  40. */
  41. private $bankAccountOwnerName;
  42. /**
  43. * @var string
  44. */
  45. private $bankAccountIbanNumber;
  46. /**
  47. * @var string
  48. */
  49. private $bankAccountBicSwiftNumber;
  50. /**
  51. * @var WhaterOrganization
  52. */
  53. protected $whaterOrganization;
  54. /**
  55. * @var float
  56. */
  57. protected $money;
  58. /**
  59. * @var float
  60. */
  61. protected $coins;
  62. /**
  63. * @var string
  64. */
  65. protected $currency;
  66. /**
  67. * @var string
  68. */
  69. protected $privateNote;
  70. /**
  71. * @var string
  72. */
  73. protected $publicNote;
  74. /**
  75. * @var \DateTime
  76. */
  77. protected $createdAt;
  78. /**
  79. * @var WalletOperation
  80. */
  81. protected $walletOperation;
  82. /**
  83. * WalletLiquidationRequest constructor.
  84. */
  85. public function __construct(
  86. string $walletLiquidationRequestId = null,
  87. string $requestType,
  88. WhaterOrganization $whaterOrganization,
  89. float $money,
  90. float $coins,
  91. string $currency = WalletOperation::WO_CURRENCY_EURO
  92. ) {
  93. try {
  94. $this->uuid = Uuid::fromString($walletLiquidationRequestId ?: Uuid::uuid4())->toString();
  95. } catch (\InvalidArgumentException $e) {
  96. throw new InvalidUUIDException();
  97. }
  98. $this->whaterOrganization = $whaterOrganization;
  99. if ($money < 0 || $coins < 0) {
  100. throw new InvalidWalletLiquidationRequestMoneyException();
  101. }
  102. $this->money = $money;
  103. $this->coins = $coins;
  104. $this->status = WalletLiquidationRequest::WLR_STATUS_PENDING;
  105. switch ($requestType) {
  106. case WalletLiquidationRequest::WLR_TYPE_REQUEST_AUTOMATICALLY:
  107. case WalletLiquidationRequest::WLR_TYPE_REQUEST_BY_ADMIN:
  108. case WalletLiquidationRequest::WLR_TYPE_REQUEST_BY_ORGANIZATION:
  109. $this->requestType = $requestType;
  110. break;
  111. default:
  112. throw new InvalidWalletLiquidationRequestTypeException();
  113. }
  114. if ($currency = !null) {
  115. switch ($currency) {
  116. case WalletOperation::WO_CURRENCY_EURO:
  117. $this->currency = $currency;
  118. break;
  119. default:
  120. throw new InvalidWalletLiquidationRequestCurrencyException();
  121. }
  122. }
  123. $this->createdAt = new \DateTime();
  124. }
  125. public function updateRequest(
  126. string $bankAccountOwnerName = null,
  127. string $bankAccountIbanNumber = null,
  128. string $bankAccountBicSwiftNumber = null
  129. ) {
  130. $this->bankAccountOwnerName = $bankAccountOwnerName;
  131. $this->bankAccountIbanNumber = $bankAccountIbanNumber;
  132. $this->bankAccountBicSwiftNumber = $bankAccountBicSwiftNumber;
  133. return $this;
  134. }
  135. public function updateStatus(
  136. string $status,
  137. float $money,
  138. float $coins,
  139. \DateTime $liquidationDate = null,
  140. WalletOperation $walletOperation = null
  141. ) {
  142. if ($money < 0 || $coins < 0) {
  143. throw new InvalidWalletLiquidationRequestMoneyException();
  144. }
  145. if ($this->status() != WalletLiquidationRequest::WLR_STATUS_CONFIRMED) {
  146. $this->money = $money;
  147. $this->coins = $coins;
  148. }
  149. $this->walletOperation = $walletOperation;
  150. switch ($status) {
  151. case WalletLiquidationRequest::WLR_STATUS_PENDING:
  152. case WalletLiquidationRequest::WLR_STATUS_CANCELED:
  153. $this->money = $money;
  154. $this->coins = $coins;
  155. $this->status = $status;
  156. break;
  157. case WalletLiquidationRequest::WLR_STATUS_CONFIRMED:
  158. if ($this->liquidationDate == null) {
  159. $this->liquidationDate = new \DateTime();
  160. }
  161. if ($this->walletOperation == null && $this->status == WalletLiquidationRequest::WLR_STATUS_CONFIRMED) {
  162. throw new InvalidWalletLiquidationRequestStatusException();
  163. }
  164. $this->status = $status;
  165. break;
  166. default:
  167. throw new InvalidWalletLiquidationRequestTypeException();
  168. }
  169. $this->liquidationDate = $liquidationDate;
  170. return $this;
  171. }
  172. public function updateNotes(
  173. string $privateNote = null,
  174. string $publicNote = null
  175. ) {
  176. $this->privateNote = $privateNote;
  177. $this->publicNote = $publicNote;
  178. return $this;
  179. }
  180. /**
  181. * @return string
  182. */
  183. public function id(): string
  184. {
  185. return $this->uuid;
  186. }
  187. /**
  188. * @return WhaterOrganization
  189. */
  190. public function whaterOrganization(): ?WhaterOrganization
  191. {
  192. return $this->whaterOrganization;
  193. }
  194. /**
  195. * @return \DateTime
  196. */
  197. public function liquidationDate(): ?\DateTime
  198. {
  199. return $this->liquidationDate;
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function status(): string
  205. {
  206. return $this->status;
  207. }
  208. /**
  209. * @return string
  210. */
  211. public function requestType(): string
  212. {
  213. return $this->requestType;
  214. }
  215. /**
  216. * @return float
  217. */
  218. public function money(): float
  219. {
  220. return $this->money;
  221. }
  222. /**
  223. * @return float
  224. */
  225. public function coins(): float
  226. {
  227. return $this->coins;
  228. }
  229. /**
  230. * @return string
  231. */
  232. public function currency(): string
  233. {
  234. return $this->currency;
  235. }
  236. /**
  237. * @return string
  238. */
  239. public function bankAccountOwnerName()
  240. {
  241. return $this->bankAccountOwnerName;
  242. }
  243. /**
  244. * @return string
  245. */
  246. public function bankAccountIbanNumber()
  247. {
  248. return $this->bankAccountIbanNumber;
  249. }
  250. /**
  251. * @return string
  252. */
  253. public function bankAccountBicSwiftNumber()
  254. {
  255. return $this->bankAccountBicSwiftNumber;
  256. }
  257. /**
  258. * @return string
  259. */
  260. public function privateNote(): ?string
  261. {
  262. return $this->privateNote;
  263. }
  264. /**
  265. * @return string
  266. */
  267. public function publicNote(): ?string
  268. {
  269. return $this->publicNote;
  270. }
  271. /**
  272. * @return WalletOperation
  273. */
  274. public function walletOperation(): ?WalletOperation
  275. {
  276. return $this->walletOperation;
  277. }
  278. /**
  279. * @return \DateTime
  280. */
  281. public function createdAt(): \DateTime
  282. {
  283. return $this->createdAt;
  284. }
  285. }