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

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