src/Domain/User/Model/OrganizationLicenseFee.php line 16

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\User\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Whater\Domain\Common\Exception\InvalidUUIDException;
  6. use Ramsey\Uuid\Uuid;
  7. use Whater\Domain\User\Exception\InvalidLicenseTypeException;
  8. /**
  9. * Class OrganizationLicenseFee
  10. *
  11. * @package Whater\Domain\User\Model
  12. */
  13. class OrganizationLicenseFee implements ContainsRecordedEvents
  14. {
  15. use EventRecorderCapabilities;
  16. /**
  17. * @var string
  18. */
  19. private $uuid;
  20. /**
  21. * @var \DateTime
  22. */
  23. private $createdAt;
  24. /**
  25. * @var \DateTime
  26. */
  27. private $updatedAt;
  28. /**
  29. * @var string
  30. */
  31. private $licenseType;
  32. /**
  33. * @var int
  34. */
  35. private $maximumPopulation;
  36. /**
  37. * @var string
  38. */
  39. private $establishmentType;
  40. /**
  41. * @var string
  42. */
  43. private $licenseScope;
  44. /**
  45. * @var float
  46. */
  47. private $licenseFee;
  48. /**
  49. * @var string
  50. */
  51. private $paymentPlan;
  52. /**
  53. * @var string
  54. */
  55. private $checksum;
  56. public function __construct(
  57. string $organizationLicenseFeeId = null,
  58. string $licenseType,
  59. string $licenseScope,
  60. float $licenseFee,
  61. string $paymentPlan,
  62. int $maximumPopulation = null,
  63. string $establishmentType = null
  64. ) {
  65. try {
  66. $this->uuid = Uuid::fromString($organizationLicenseFeeId ?: Uuid::uuid4())->toString();
  67. } catch (\InvalidArgumentException $e) {
  68. throw new InvalidUUIDException();
  69. }
  70. switch ($licenseType) {
  71. case OrganizationLicense::LICENSE_TYPE_BASIC:
  72. case OrganizationLicense::LICENSE_TYPE_STANDARD:
  73. case OrganizationLicense::LICENSE_TYPE_PREMIUM:
  74. $this->licenseType = $licenseType;
  75. break;
  76. default:
  77. throw new InvalidLicenseTypeException();
  78. }
  79. switch ($licenseScope) {
  80. case OrganizationLicense::LICENSE_SCOPE_ESTABLISHMENTS:
  81. $this->establishmentType = $establishmentType;
  82. case OrganizationLicense::LICENSE_SCOPE_TOWNS:
  83. case OrganizationLicense::LICENSE_SCOPE_COUNTRIES_COUNTRY_AREAS_AGRUPATIONS:
  84. case OrganizationLicense::LICENSE_SCOPE_PROFESSIONALS:
  85. case OrganizationLicense::LICENSE_SCOPE_SELLERS:
  86. $this->licenseScope = $licenseScope;
  87. break;
  88. default:
  89. throw new InvalidLicenseTypeException();
  90. }
  91. $this->licenseFee = $licenseFee;
  92. if ($this->licenseFee < 0) {
  93. $this->licenseFee = 0;
  94. }
  95. switch ($paymentPlan) {
  96. case OrganizationLicense::LICENSE_PAYMENT_PLAN_MONTHLY:
  97. case OrganizationLicense::LICENSE_PAYMENT_PLAN_ANNUAL:
  98. $this->paymentPlan = $paymentPlan;
  99. break;
  100. default:
  101. throw new InvalidLicenseTypeException();
  102. }
  103. $this->maximumPopulation = $maximumPopulation;
  104. if ($this->maximumPopulation != null && $this->maximumPopulation < 0) {
  105. $this->maximumPopulation = 0;
  106. }
  107. //Calculate checksum
  108. $hash = md5("LT" . $this->licenseType);
  109. $hash = md5($hash . "PP" . $this->paymentPlan);
  110. $hash = md5($hash . "LS" . $this->licenseScope);
  111. if ($this->maximumPopulation != null) {
  112. $hash = md5($hash . "MP" . $this->maximumPopulation);
  113. }
  114. if ($this->establishmentType != null) {
  115. $hash = md5($hash . "ET" . $this->establishmentType);
  116. }
  117. $this->checksum = $hash;
  118. $this->createdAt = new \DateTime();
  119. $this->updatedAt = new \DateTime();
  120. }
  121. public function editLicenseFee(
  122. string $licenseType,
  123. float $licenseFee,
  124. string $paymentPlan,
  125. int $maximumPopulation = null,
  126. string $establishmentType = null
  127. ) {
  128. switch ($licenseType) {
  129. case OrganizationLicense::LICENSE_TYPE_BASIC:
  130. case OrganizationLicense::LICENSE_TYPE_STANDARD:
  131. case OrganizationLicense::LICENSE_TYPE_PREMIUM:
  132. $this->licenseType = $licenseType;
  133. break;
  134. default:
  135. throw new InvalidLicenseTypeException();
  136. }
  137. $this->licenseFee = $licenseFee;
  138. if ($this->licenseFee < 0) {
  139. $this->licenseFee = 0;
  140. }
  141. switch ($paymentPlan) {
  142. case OrganizationLicense::LICENSE_PAYMENT_PLAN_MONTHLY:
  143. case OrganizationLicense::LICENSE_PAYMENT_PLAN_ANNUAL:
  144. $this->paymentPlan = $paymentPlan;
  145. break;
  146. default:
  147. throw new InvalidLicenseTypeException();
  148. }
  149. $this->maximumPopulation = $maximumPopulation;
  150. if ($this->maximumPopulation != null && $this->maximumPopulation < 0) {
  151. $this->maximumPopulation = 0;
  152. }
  153. if ($this->licenseScope == OrganizationLicense::LICENSE_SCOPE_ESTABLISHMENTS) {
  154. $this->establishmentType = $establishmentType;
  155. }
  156. //Calculate checksum
  157. $hash = md5("LT" . $this->licenseType);
  158. $hash = md5($hash . "PP" . $this->paymentPlan);
  159. $hash = md5($hash . "LS" . $this->licenseScope);
  160. if ($this->maximumPopulation != null) {
  161. $hash = md5($hash . "MP" . $this->maximumPopulation);
  162. }
  163. $this->checksum = $hash;
  164. $this->updatedAt = new \DateTime();
  165. }
  166. /**
  167. * @return string
  168. */
  169. public function id(): string
  170. {
  171. return $this->uuid;
  172. }
  173. /**
  174. * @return string
  175. */
  176. public function licenseType(): string
  177. {
  178. return $this->licenseType;
  179. }
  180. /**
  181. * @return string
  182. */
  183. public function licenseScope(): string
  184. {
  185. return $this->licenseScope;
  186. }
  187. /**
  188. * @return string
  189. */
  190. public function establishmentType(): ?string
  191. {
  192. return $this->establishmentType;
  193. }
  194. /**
  195. * @return float
  196. */
  197. public function licenseFee(): float
  198. {
  199. return $this->licenseFee;
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function paymentPlan(): string
  205. {
  206. return $this->paymentPlan;
  207. }
  208. /**
  209. * @return int
  210. */
  211. public function maximumPopulation(): ?int
  212. {
  213. return $this->maximumPopulation;
  214. }
  215. /**
  216. * @return string
  217. */
  218. public function checksum(): string
  219. {
  220. return $this->checksum;
  221. }
  222. /**
  223. * @return \DateTime
  224. */
  225. public function createdAt(): \DateTime
  226. {
  227. return $this->createdAt;
  228. }
  229. /**
  230. * @return \DateTime|null
  231. */
  232. public function updatedAt()
  233. {
  234. return $this->updatedAt;
  235. }
  236. /**
  237. * @return bool
  238. */
  239. public function equals(OrganizationLicenseFee $organizationLicenseFee)
  240. {
  241. return $this->id() === $organizationLicenseFee->id();
  242. }
  243. }