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