You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.8 KiB

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