src/Domain/User/Model/User.php line 36

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 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\Security\ValueObject\AuthUser;
  10. use Whater\Domain\Security\ValueObject\EncodedPasswordInterface;
  11. use Assert\Assertion;
  12. use Whater\Infrastructure\SecurityBundle\ValueObject\EncodedPassword;
  13. use Whater\Domain\Media\Model\Media;
  14. use Whater\Domain\Product\Model\Product;
  15. use Whater\Domain\Product\Model\WalletOperation;
  16. use Whater\Domain\User\Exception\IllegalPermissionObjectException;
  17. use Whater\Domain\User\Exception\InvalidPermissionException;
  18. use Whater\Domain\User\Exception\UserOrganizationAlreadyExistsException;
  19. use Whater\Domain\Whater\Model\DistributionNetwork;
  20. use Whater\Domain\Whater\Model\DistributionNetworkValoration;
  21. use Whater\Domain\Whater\Model\WhaterOrganization;
  22. use Whater\Domain\Whater\Model\WhaterPoint;
  23. use Whater\Domain\Whater\Model\WhaterValoration;
  24. use Whater\Domain\Zones\Model\CountryArea;
  25. use Whater\Domain\Zones\Model\Town;
  26. use Whater\Domain\Zones\Model\TownLocation;
  27. use Whater\Domain\Zones\Model\Ubication;
  28. /**
  29. * Class User
  30. *
  31. * @package Whater\Domain\User\Model
  32. */
  33. class User implements ContainsRecordedEvents
  34. {
  35. use EventRecorderCapabilities;
  36. const DEFAULT_ROLES = [
  37. Role::ROLE_USER
  38. ];
  39. /**
  40. * @var string
  41. */
  42. private $uuid;
  43. /**
  44. * @var string
  45. */
  46. private $email;
  47. /**
  48. * @var AuthUser
  49. */
  50. private $auth;
  51. /**
  52. * @var \DateTime
  53. */
  54. private $createdAt;
  55. /**
  56. * @var null|\DateTime
  57. */
  58. private $updatedAt;
  59. /**
  60. * @var Collection
  61. */
  62. private $roles;
  63. /**
  64. * @var \Whater\Domain\User\Model\Role
  65. */
  66. private $defaultRole;
  67. /**
  68. * @var \Whater\Domain\User\Model\Role
  69. */
  70. private $activeRole;
  71. /**
  72. * @var bool
  73. */
  74. private $isEnabled;
  75. /**
  76. * @var string
  77. */
  78. private $firstName;
  79. /**
  80. * @var string
  81. */
  82. private $lastName;
  83. /**
  84. * @var string
  85. */
  86. private $phone;
  87. /**
  88. * @var boolean
  89. */
  90. private $isDeleted;
  91. /**
  92. * @var string
  93. */
  94. private $plainPassword;
  95. /**
  96. * @var string
  97. */
  98. private $resetTokenHash;
  99. /**
  100. * @var \DateTime
  101. */
  102. private $resetTokenExpireAt;
  103. /**
  104. * @var Collection
  105. */
  106. private $organizationLicenses;
  107. /**
  108. * @var Collection
  109. */
  110. private $valorationsOfWhaterPoints;
  111. /**
  112. * @var Collection
  113. */
  114. private $valorationsOfDistributionNetworks;
  115. /**
  116. * @var Collection
  117. */
  118. private $productValorations;
  119. /**
  120. * @var Collection
  121. */
  122. private $cartOrders;
  123. /**
  124. * @var Collection
  125. */
  126. private $buyerWalletOperations;
  127. /**
  128. * @var Collection
  129. */
  130. private $createdWhaterPoints;
  131. /**
  132. * @var Collection
  133. */
  134. private $uploadedImportDataFiles;
  135. /**
  136. * @var Town
  137. */
  138. private $town;
  139. /**
  140. * @var Media
  141. */
  142. private $avatarImage;
  143. /**
  144. * @var string
  145. */
  146. private $publicComments;
  147. /**
  148. * @var string
  149. */
  150. private $privateComments;
  151. /**
  152. * @var WhaterOrganization
  153. */
  154. private $whaterOrganization;
  155. /**
  156. * @var Collection
  157. */
  158. private $myLocations;
  159. /**
  160. * @var Collection
  161. */
  162. private $myEstablishments;
  163. /**
  164. * @var Collection
  165. */
  166. private $notifications;
  167. /**
  168. * @var int
  169. */
  170. private $unreadNotifications;
  171. /**
  172. * @var float
  173. */
  174. private $currentBalance;
  175. /**
  176. * @var Collection
  177. */
  178. private $ownershipRequests;
  179. /**
  180. * @var string
  181. */
  182. private $stripeCustomerId;
  183. /**
  184. * @var string
  185. */
  186. private $referralCodeUsed;
  187. /**
  188. * @var WhaterOrganization
  189. */
  190. private $referralWhaterOrganization;
  191. /**
  192. * @var Collection
  193. */
  194. private $whatercoinsCoupons;
  195. /**
  196. * User constructor.
  197. * @param string|null $userId
  198. * @param string $username
  199. * @param string $email
  200. * @param null|EncodedPasswordInterface|null $encodedPassword
  201. * @param bool $enabled
  202. */
  203. public function __construct(
  204. string $userId = null,
  205. string $username,
  206. string $email,
  207. ?EncodedPasswordInterface $encodedPassword = null,
  208. bool $isEnabled = true,
  209. string $publicComments = null,
  210. string $privateComments = null,
  211. Media $avatarImage = null,
  212. string $referralCodeUsed = null
  213. ) {
  214. try {
  215. $this->uuid = Uuid::fromString($userId ?: Uuid::uuid4())->toString();
  216. } catch (\InvalidArgumentException $e) {
  217. throw new InvalidUUIDException();
  218. }
  219. if ($encodedPassword == null) {
  220. $this->plainPassword = $this->generatePlainPassword();
  221. $encodedPassword = new EncodedPassword($this->plainPassword);
  222. }
  223. $this->auth = new AuthUser($username, $encodedPassword);
  224. $this->setEmail($email);
  225. $this->avatarImage = $avatarImage;
  226. $this->publicComments = $publicComments;
  227. $this->privateComments = $privateComments;
  228. $this->createdAt = new \DateTime();
  229. $this->updatedAt = new \DateTime();
  230. $this->roles = new ArrayCollection();
  231. $this->cartOrders = new ArrayCollection();
  232. $this->organizationLicenses = new ArrayCollection();
  233. $this->valorationsOfWhaterPoints = new ArrayCollection();
  234. $this->valorationsOfDistributionNetworks = new ArrayCollection();
  235. $this->productValorations = new ArrayCollection();
  236. $this->buyerWalletOperations = new ArrayCollection();
  237. $this->createdWhaterPoints = new ArrayCollection();
  238. $this->uploadedImportDataFiles = new ArrayCollection();
  239. $this->whatercoinsCoupons = new ArrayCollection();
  240. $this->isEnabled = $isEnabled;
  241. $this->isDeleted = false;
  242. $this->currentBalance = 0;
  243. $this->myLocations = new ArrayCollection();
  244. $this->myEstablishments = new ArrayCollection();
  245. $this->notifications = new ArrayCollection();
  246. $this->ownershipRequests = new ArrayCollection();
  247. $this->referralCodeUsed = $referralCodeUsed;
  248. }
  249. public function __toString()
  250. {
  251. return $this->fullUsername(true);
  252. }
  253. public function fullUsername(?bool $showusername = false): string
  254. {
  255. $fullname = $this->firstName . ' ' . $this->lastName;
  256. if (trim($fullname) == '') {
  257. return $this->username();
  258. }
  259. if ($showusername) {
  260. $fullname .= ' (' . $this->username() . ')';
  261. }
  262. return $fullname;
  263. }
  264. public function registerReferralWhaterOrganization(
  265. WhaterOrganization $whaterOrganization = null
  266. ) {
  267. $this->referralWhaterOrganization = $whaterOrganization;
  268. }
  269. /**
  270. * Update User
  271. *
  272. * @param null|string $email
  273. * @param EncodedPasswordInterface|null $encodedPassword
  274. * @param null|string $firstName
  275. * @param null|string $lastName
  276. * @param null|string $phone
  277. *
  278. * @return $this
  279. */
  280. public function updateUser(
  281. ?string $email = '',
  282. ?EncodedPasswordInterface $encodedPassword = null,
  283. ?string $firstName = '',
  284. ?string $lastName = '',
  285. ?string $phone = '',
  286. ?string $publicComments = '',
  287. ?string $privateComments = '',
  288. ?Media $avatarImage = null
  289. ) {
  290. if (!empty($email)) {
  291. $this->setEmail($email);
  292. }
  293. if (!empty($encodedPassword)) {
  294. $this->auth = new AuthUser($this->email(), $encodedPassword);
  295. }
  296. if (!empty($firstName)) {
  297. $this->firstName = $firstName;
  298. }
  299. if (!empty($lastName)) {
  300. $this->lastName = $lastName;
  301. }
  302. if (!empty($phone)) {
  303. $this->phone = $phone;
  304. }
  305. if (!empty($publicComments)) {
  306. $this->publicComments = $publicComments;
  307. }
  308. if (!empty($privateComments)) {
  309. $this->privateComments = $privateComments;
  310. }
  311. if (!empty($avatarImage)) {
  312. $this->avatarImage = $avatarImage;
  313. }
  314. $this->updatedAt = new \DateTime();
  315. return $this;
  316. }
  317. public function updateUserTown(
  318. Town $town
  319. ) {
  320. $this->town = $town;
  321. $this->updatedAt = new \DateTime();
  322. return $this;
  323. }
  324. public function updateOrganization(
  325. WhaterOrganization $whaterOrganization = null
  326. ) {
  327. if ($whaterOrganization == null) {
  328. $this->whaterOrganization = null;
  329. $this->updatedAt = new \DateTime();
  330. } else if ($this->whaterOrganization() == null) {
  331. $this->whaterOrganization = $whaterOrganization;
  332. $this->updatedAt = new \DateTime();
  333. } else {
  334. throw new UserOrganizationAlreadyExistsException();
  335. }
  336. return $this;
  337. }
  338. public function hasActiveLicense(string $licenseScope, string $licenseType = null): bool
  339. {
  340. if ($this->hasRole(Role::ROLE_ADMIN)) {
  341. return true;
  342. }
  343. if ($this->whaterOrganization() != null && $this->whaterOrganization()->hasActiveLicense($licenseScope, $licenseType)) {
  344. return true;
  345. }
  346. return false;
  347. }
  348. public function checkPermission(string $permissionCode, $object = null)
  349. {
  350. switch ($permissionCode) {
  351. case UserPermission::PERMISSION_CREATE_PRODUCT:
  352. if ($object != null) {
  353. throw new IllegalPermissionObjectException();
  354. }
  355. if ($this->hasActiveLicense(OrganizationLicense::LICENSE_SCOPE_SELLERS)) {
  356. return true;
  357. }
  358. break;
  359. case UserPermission::PERMISSION_EDIT_PRODUCT:
  360. if ($object == null || !($object instanceof Product)) {
  361. throw new IllegalPermissionObjectException();
  362. }
  363. if ($this->hasActiveLicense(OrganizationLicense::LICENSE_SCOPE_SELLERS)) {
  364. return true;
  365. }
  366. break;
  367. case UserPermission::PERMISSION_EDIT_WHATER_POINT:
  368. if ($object == null || !($object instanceof WhaterPoint)) {
  369. throw new IllegalPermissionObjectException();
  370. }
  371. /** @var WhaterPoint $whaterpoint */
  372. $whaterpoint = $object;
  373. if (
  374. $whaterpoint->createdBy() != null &&
  375. $whaterpoint->createdBy()->equals($this)
  376. ) {
  377. return true;
  378. }
  379. if ($this->whaterOrganization() != null) {
  380. if ($this->whaterOrganization()->checkPermission($permissionCode, $object)) {
  381. return true;
  382. }
  383. }
  384. break;
  385. case UserPermission::PERMISSION_CREATE_ANALYTICAL:
  386. throw new \Exception($permissionCode . 'need code implementation!');
  387. break;
  388. case UserPermission::PERMISSION_EDIT_ANALYTICAL:
  389. throw new \Exception($permissionCode . 'need code implementation!');
  390. break;
  391. case UserPermission::PERMISSION_EDIT_WHATER_ORGANIZATION:
  392. throw new \Exception($permissionCode . 'need code implementation!');
  393. break;
  394. case UserPermission::PERMISSION_CREATE_DISTRIBUTION_NETWORK:
  395. throw new \Exception($permissionCode . 'need code implementation!');
  396. break;
  397. case UserPermission::PERMISSION_EDIT_DISTRIBUTION_NETWORK:
  398. if ($object == null || !($object instanceof DistributionNetwork)) {
  399. throw new IllegalPermissionObjectException();
  400. }
  401. if ($this->whaterOrganization() != null) {
  402. if ($this->whaterOrganization()->checkPermission($permissionCode, $object)) {
  403. return true;
  404. }
  405. }
  406. break;
  407. case UserPermission::PERMISSION_CREATE_WHATER_DEVICE:
  408. throw new \Exception($permissionCode . 'need code implementation!');
  409. break;
  410. case UserPermission::PERMISSION_EDIT_WHATER_DEVICE:
  411. throw new \Exception($permissionCode . 'need code implementation!');
  412. break;
  413. case UserPermission::PERMISSION_EDIT_COUNTRY:
  414. throw new \Exception($permissionCode . 'need code implementation!');
  415. break;
  416. case UserPermission::PERMISSION_CREATE_COUNTRY_AREA:
  417. throw new \Exception($permissionCode . 'need code implementation!');
  418. break;
  419. case UserPermission::PERMISSION_EDIT_COUNTRY_AREA:
  420. if ($object == null || !($object instanceof CountryArea)) {
  421. throw new IllegalPermissionObjectException();
  422. }
  423. if ($this->whaterOrganization() != null) {
  424. if ($this->whaterOrganization()->checkPermission($permissionCode, $object)) {
  425. return true;
  426. }
  427. }
  428. break;
  429. case UserPermission::PERMISSION_CREATE_TOWN:
  430. throw new \Exception($permissionCode . 'need code implementation!');
  431. break;
  432. case UserPermission::PERMISSION_EDIT_TOWN:
  433. if ($object == null || !($object instanceof Town)) {
  434. throw new IllegalPermissionObjectException();
  435. }
  436. if ($this->whaterOrganization() != null) {
  437. if ($this->whaterOrganization()->checkPermission($permissionCode, $object)) {
  438. return true;
  439. }
  440. }
  441. break;
  442. case UserPermission::PERMISSION_CREATE_UBICATION:
  443. throw new \Exception($permissionCode . 'need code implementation!');
  444. break;
  445. case UserPermission::PERMISSION_EDIT_UBICATION:
  446. if ($object == null || !($object instanceof Ubication)) {
  447. throw new IllegalPermissionObjectException();
  448. }
  449. if ($this->whaterOrganization() != null) {
  450. if ($this->whaterOrganization()->checkPermission($permissionCode, $object)) {
  451. return true;
  452. }
  453. }
  454. break;
  455. case UserPermission::PERMISSION_CREATE_TOWN_LOCATION:
  456. throw new \Exception($permissionCode . 'need code implementation!');
  457. break;
  458. case UserPermission::PERMISSION_EDIT_TOWN_LOCATION:
  459. if ($object == null || !($object instanceof TownLocation)) {
  460. throw new IllegalPermissionObjectException();
  461. }
  462. if ($this->whaterOrganization() != null) {
  463. if ($this->whaterOrganization()->checkPermission($permissionCode, $object)) {
  464. return true;
  465. }
  466. }
  467. break;
  468. case UserPermission::PERMISSION_CREATE_DISTRICT:
  469. throw new \Exception($permissionCode . 'need code implementation!');
  470. break;
  471. case UserPermission::PERMISSION_EDIT_DISTRICT:
  472. throw new \Exception($permissionCode . 'need code implementation!');
  473. break;
  474. case UserPermission::PERMISSION_CREATE_ESTABLISHMENT:
  475. throw new \Exception($permissionCode . 'need code implementation!');
  476. break;
  477. case UserPermission::PERMISSION_EDIT_ESTABLISHMENT:
  478. throw new \Exception($permissionCode . 'need code implementation!');
  479. break;
  480. default:
  481. throw new InvalidPermissionException();
  482. }
  483. return false;
  484. }
  485. public function setStripeCustomerId(string $stripeCustomerId)
  486. {
  487. if (!empty($stripeCustomerId)) {
  488. $this->stripeCustomerId = $stripeCustomerId;
  489. $this->updatedAt = new \DateTime();
  490. }
  491. return $this;
  492. }
  493. /**
  494. * @return string
  495. */
  496. public function id(): string
  497. {
  498. return $this->uuid;
  499. }
  500. /**
  501. * @return string
  502. */
  503. public function email(): string
  504. {
  505. return $this->email;
  506. }
  507. /**
  508. * @param $email
  509. * @return string
  510. */
  511. protected function setEmail($email)
  512. {
  513. $email = trim($email);
  514. if (!$email) {
  515. throw new \InvalidArgumentException('email');
  516. }
  517. Assertion::email($email);
  518. $this->email = strtolower($email);
  519. $this->updatedAt = new \DateTime();
  520. return $email;
  521. }
  522. /**
  523. * @return Media
  524. */
  525. public function avatarImage(): ?Media
  526. {
  527. return $this->avatarImage;
  528. }
  529. /**
  530. * @return string
  531. */
  532. public function username(): string
  533. {
  534. if (empty($this->auth)) {
  535. return '';
  536. }
  537. return $this->auth->username();
  538. }
  539. /**
  540. * @return AuthUser
  541. */
  542. public function auth(): AuthUser
  543. {
  544. return $this->auth;
  545. }
  546. /**
  547. * @return \DateTime
  548. */
  549. public function createdAt(): \DateTime
  550. {
  551. return $this->createdAt;
  552. }
  553. /**
  554. * @return \DateTime|null
  555. */
  556. public function updatedAt()
  557. {
  558. return $this->updatedAt;
  559. }
  560. /**
  561. * @return Collection
  562. */
  563. public function roles(): Collection
  564. {
  565. return $this->roles;
  566. }
  567. /**
  568. * @return array
  569. */
  570. public function rolesAsArray(): array
  571. {
  572. $roles = self::DEFAULT_ROLES;
  573. foreach ($this->roles as $role) {
  574. if (!$role->isEnabled()) {
  575. continue;
  576. }
  577. if (!in_array($role->type(), $roles)) {
  578. $roles[] = $role->type();
  579. }
  580. $baseRole = Role::baseRole($role->type());
  581. if (!in_array($baseRole, $roles)) {
  582. $roles[] = $baseRole;
  583. }
  584. }
  585. return $roles;
  586. }
  587. /**
  588. * return bool
  589. */
  590. public function hasRole($role): bool
  591. {
  592. $roles = $this->rolesAsArray();
  593. foreach ($roles as $userRole) {
  594. if ($role === $userRole) {
  595. return true;
  596. }
  597. }
  598. return false;
  599. }
  600. /**
  601. * @return bool
  602. */
  603. public function equals(User $user)
  604. {
  605. return $this->id() === $user->id();
  606. }
  607. /**
  608. * @return Role
  609. */
  610. public function defaultRole()
  611. {
  612. return $this->defaultRole;
  613. }
  614. /**
  615. * Assign default role
  616. */
  617. public function assignDefaultRole(Role $role)
  618. {
  619. if (!$this->hasRole($role->type())) {
  620. $this->roles()->add($role);
  621. }
  622. $this->defaultRole = $role;
  623. }
  624. /**
  625. * @return Role
  626. */
  627. public function activeRole()
  628. {
  629. if (empty($this->activeRole)) {
  630. return $this->defaultRole;
  631. }
  632. return $this->activeRole;
  633. }
  634. /**
  635. * Assign active role
  636. */
  637. public function assignActiveRole(Role $role)
  638. {
  639. if (!$this->hasRole($role->type())) {
  640. $this->roles()->add($role);
  641. }
  642. $this->activeRole = $role;
  643. }
  644. /**
  645. * @return string
  646. */
  647. public function resetTokenHash(): ?string
  648. {
  649. return $this->resetTokenHash;
  650. }
  651. /**
  652. * @return \DateTime
  653. */
  654. public function resetTokenExpireAt(): ?\DateTime
  655. {
  656. return $this->resetTokenExpireAt;
  657. }
  658. /**
  659. * @return bool
  660. */
  661. public function isEnabled()
  662. {
  663. return (bool) $this->isEnabled;
  664. }
  665. /**
  666. * set Enable
  667. */
  668. public function enableUser()
  669. {
  670. $this->isEnabled = true;
  671. }
  672. /**
  673. * set Disable
  674. */
  675. public function disableUser()
  676. {
  677. $this->isEnabled = false;
  678. }
  679. /**
  680. * Saldo disponible (ingresos por ventas - comisiones por ventas - pagos a cc )
  681. * @return float
  682. */
  683. public function currentBalance(): float
  684. {
  685. return $this->currentBalance;
  686. }
  687. /**
  688. * @return float
  689. * @return bool
  690. */
  691. public function updateWallet()
  692. {
  693. $balance = 0;
  694. foreach ($this->buyerWalletOperations() as $buyerWalletOperation) {
  695. if ($buyerWalletOperation->checkWalletOperationChecksum()) {
  696. $balance = $balance + $buyerWalletOperation->buyerUserCoins();
  697. }
  698. }
  699. $this->currentBalance = $balance;
  700. }
  701. /**
  702. * @return string
  703. */
  704. public function firstName()
  705. {
  706. return $this->firstName;
  707. }
  708. /**
  709. * @return string
  710. */
  711. public function lastName()
  712. {
  713. return $this->lastName;
  714. }
  715. /**
  716. * @return string
  717. */
  718. public function publicComments()
  719. {
  720. return $this->publicComments;
  721. }
  722. /**
  723. * @return string
  724. */
  725. public function privateComments()
  726. {
  727. return $this->privateComments;
  728. }
  729. /**
  730. * @return Town
  731. */
  732. public function town()
  733. {
  734. return $this->town;
  735. }
  736. /**
  737. * @return string
  738. */
  739. public function fullName()
  740. {
  741. if (empty($this->lastName)) {
  742. return $this->firstName();
  743. } else {
  744. return $this->firstName() . ' ' . $this->lastName();
  745. }
  746. }
  747. /**
  748. * @return bool
  749. */
  750. public function isDeleted(): bool
  751. {
  752. return $this->isDeleted;
  753. }
  754. /**
  755. * @return WhaterOrganization|null
  756. */
  757. public function whaterOrganization(): ?WhaterOrganization
  758. {
  759. return $this->whaterOrganization;
  760. }
  761. /**
  762. * @return Collection
  763. */
  764. public function valorationsOfWhaterPoints(): Collection
  765. {
  766. return $this->valorationsOfWhaterPoints;
  767. }
  768. /**
  769. * @return Collection
  770. */
  771. public function valorationsOfDistributionNetworks(): Collection
  772. {
  773. return $this->valorationsOfDistributionNetworks;
  774. }
  775. /**
  776. * @return Collection
  777. */
  778. public function productValorations(): Collection
  779. {
  780. return $this->productValorations;
  781. }
  782. /**
  783. * @return WhaterValoration
  784. */
  785. public function valorationForWhaterpoint(string $whaterpointId): ?WhaterValoration
  786. {
  787. foreach ($this->valorationsOfWhaterpoints() as $valoration) {
  788. if ($valoration->whaterPoint()->id() == $whaterpointId) {
  789. return $valoration;
  790. }
  791. }
  792. return null;
  793. }
  794. /**
  795. * @return DistributionNetworkValoration
  796. */
  797. public function valorationForDistributionNetwork(string $distributionNetworkId): ?DistributionNetworkValoration
  798. {
  799. foreach ($this->valorationsOfDistributionNetworks() as $valoration) {
  800. if ($valoration->distributionNetwork()->id() == $distributionNetworkId) {
  801. return $valoration;
  802. }
  803. }
  804. return null;
  805. }
  806. /**
  807. * @return Collection
  808. */
  809. public function buyerWalletOperations(): Collection
  810. {
  811. return $this->buyerWalletOperations;
  812. }
  813. /**
  814. * @return Collection
  815. */
  816. public function createdWhaterPoints(): Collection
  817. {
  818. return $this->createdWhaterPoints;
  819. }
  820. /**
  821. * @return Collection
  822. */
  823. public function uploadedImportDataFiles(): Collection
  824. {
  825. return $this->uploadedImportDataFiles;
  826. }
  827. /**
  828. * @return Collection
  829. */
  830. public function whatercoinsCoupons(): Collection
  831. {
  832. return $this->whatercoinsCoupons;
  833. }
  834. /**
  835. * @return null|Collection
  836. */
  837. public function myLocations(): ?Collection
  838. {
  839. return $this->myLocations;
  840. }
  841. /**
  842. * @return null|Collection
  843. */
  844. public function cartOrders(): ?Collection
  845. {
  846. return $this->cartOrders;
  847. }
  848. /**
  849. * @return null|Collection
  850. */
  851. public function myEstablishments(): ?Collection
  852. {
  853. return $this->myEstablishments;
  854. }
  855. /**
  856. * @return Collection
  857. */
  858. public function notifications(): Collection
  859. {
  860. if (!$this->notifications) {
  861. $this->notifications = new ArrayCollection();
  862. }
  863. return $this->notifications;
  864. }
  865. /**
  866. * @return Collection
  867. */
  868. public function ownershipRequests(): Collection
  869. {
  870. if (!$this->ownershipRequests) {
  871. $this->ownershipRequests = new ArrayCollection();
  872. }
  873. return $this->ownershipRequests;
  874. }
  875. /**
  876. * @return int
  877. */
  878. public function unreadNotifications(bool $updateUnreadNotifications = false): int
  879. {
  880. if ($updateUnreadNotifications) {
  881. $this->updateUnreadNotifications();
  882. }
  883. return $this->unreadNotifications;
  884. }
  885. public function updateUnreadNotifications(): void
  886. {
  887. $unreadNotifications = 0;
  888. foreach ($this->notifications() as $notification) {
  889. if ($notification->readAt() == null) {
  890. $unreadNotifications += 1;
  891. }
  892. }
  893. $this->unreadNotifications = $unreadNotifications;
  894. }
  895. public function addNotification($notification)
  896. {
  897. if (!$this->notifications) {
  898. $this->notifications = new ArrayCollection();
  899. }
  900. $this->notifications->add($notification);
  901. $this->updateUnreadNotifications();
  902. $this->updatedAt = new \DateTime();
  903. return $this;
  904. }
  905. /**
  906. * @return string
  907. */
  908. public function phone(): ?string
  909. {
  910. return $this->phone;
  911. }
  912. /**
  913. * @return string
  914. */
  915. public function stripeCustomerId(): ?string
  916. {
  917. return $this->stripeCustomerId;
  918. }
  919. public function avatarUrl(string $size = 'default')
  920. {
  921. switch ($size) {
  922. case 'mini':
  923. return '/assets/images/avatar_default_mini.png';
  924. case 'default':
  925. default:
  926. return '/assets/images/avatar_default.png';
  927. }
  928. }
  929. /*
  930. * Remove (logically) of the property
  931. */
  932. public function removeUser()
  933. {
  934. $this->isDeleted = true;
  935. $this->isEnabled = false;
  936. $this->email = $this->email . '@DELETE@' . time();
  937. $this->auth->removeAuth();
  938. }
  939. /**
  940. * @return string
  941. */
  942. public function plainPassword(): ?string
  943. {
  944. return $this->plainPassword;
  945. }
  946. private function generatePlainPassword($lenght = 13): string
  947. {
  948. if (function_exists("random_bytes")) {
  949. $bytes = random_bytes(ceil($lenght / 2));
  950. } elseif (function_exists("openssl_random_pseudo_bytes")) {
  951. $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2));
  952. } else {
  953. throw new \Exception("no cryptographically secure random function available");
  954. }
  955. return substr(bin2hex($bytes), 0, $lenght);
  956. }
  957. /**
  958. * @param string $ramdom should be base62 encoded (A-Z a-z 0-9) to avoid problems with the Url
  959. */
  960. public function updateResetToken(string $ramdom)
  961. {
  962. $this->resetTokenHash = md5($ramdom);
  963. $expiredAt = new \DateTime();
  964. $expiredAt = $expiredAt->modify("+1 day");
  965. $this->resetTokenExpireAt = $expiredAt;
  966. }
  967. /**
  968. * @return string
  969. */
  970. public function referralCodeUsed(): ?string
  971. {
  972. return $this->referralCodeUsed;
  973. }
  974. /**
  975. * @return WhaterOrganization
  976. */
  977. public function referralWhaterOrganization(): ?WhaterOrganization
  978. {
  979. return $this->referralWhaterOrganization;
  980. }
  981. }