GuestAvatar.php 2.7 KB

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