GuestAvatar.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Michael Weimann <mail@michael-weimann.eu>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Michael Weimann <mail@michael-weimann.eu>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Avatar;
  26. use OCP\Files\SimpleFS\ISimpleFile;
  27. use OCP\Files\SimpleFS\InMemoryFile;
  28. use Psr\Log\LoggerInterface;
  29. /**
  30. * This class represents a guest user's avatar.
  31. */
  32. class GuestAvatar extends Avatar {
  33. /**
  34. * Holds the guest user display name.
  35. */
  36. private string $userDisplayName;
  37. /**
  38. * GuestAvatar constructor.
  39. *
  40. * @param string $userDisplayName The guest user display name
  41. */
  42. public function __construct(string $userDisplayName, LoggerInterface $logger) {
  43. parent::__construct($logger);
  44. $this->userDisplayName = $userDisplayName;
  45. }
  46. /**
  47. * Tests if the user has an avatar.
  48. */
  49. public function exists(): bool {
  50. // Guests always have an avatar.
  51. return true;
  52. }
  53. /**
  54. * Returns the guest user display name.
  55. */
  56. public function getDisplayName(): string {
  57. return $this->userDisplayName;
  58. }
  59. /**
  60. * Setting avatars isn't implemented for guests.
  61. *
  62. * @param \OCP\IImage|resource|string $data
  63. * @return void
  64. */
  65. public function set($data): void {
  66. // unimplemented for guest user avatars
  67. }
  68. /**
  69. * Removing avatars isn't implemented for guests.
  70. */
  71. public function remove(bool $silent = false): void {
  72. // unimplemented for guest user avatars
  73. }
  74. /**
  75. * Generates an avatar for the guest.
  76. */
  77. public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
  78. $avatar = $this->generateAvatar($this->userDisplayName, $size, $darkTheme);
  79. return new InMemoryFile('avatar.png', $avatar);
  80. }
  81. /**
  82. * Updates the display name if changed.
  83. *
  84. * @param string $feature The changed feature
  85. * @param mixed $oldValue The previous value
  86. * @param mixed $newValue The new value
  87. */
  88. public function userChanged(string $feature, $oldValue, $newValue): void {
  89. if ($feature === 'displayName') {
  90. $this->userDisplayName = $newValue;
  91. }
  92. }
  93. /**
  94. * Guests don't have custom avatars.
  95. */
  96. public function isCustomAvatar(): bool {
  97. return false;
  98. }
  99. }