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.

GuestAvatar.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\InMemoryFile;
  27. use Psr\Log\LoggerInterface;
  28. /**
  29. * This class represents a guest user's avatar.
  30. */
  31. class GuestAvatar extends Avatar {
  32. /**
  33. * Holds the guest user display name.
  34. *
  35. * @var string
  36. */
  37. private $userDisplayName;
  38. /**
  39. * GuestAvatar constructor.
  40. *
  41. * @param string $userDisplayName The guest user display name
  42. * @param LoggerInterface $logger The logger
  43. */
  44. public function __construct(string $userDisplayName, LoggerInterface $logger) {
  45. parent::__construct($logger);
  46. $this->userDisplayName = $userDisplayName;
  47. }
  48. /**
  49. * Tests if the user has an avatar.
  50. *
  51. * @return true Guests always have an avatar.
  52. */
  53. public function exists() {
  54. return true;
  55. }
  56. /**
  57. * Returns the guest user display name.
  58. *
  59. * @return string
  60. */
  61. public function getDisplayName(): string {
  62. return $this->userDisplayName;
  63. }
  64. /**
  65. * Setting avatars isn't implemented for guests.
  66. *
  67. * @param \OCP\IImage|resource|string $data
  68. * @return void
  69. */
  70. public function set($data) {
  71. // unimplemented for guest user avatars
  72. }
  73. /**
  74. * Removing avatars isn't implemented for guests.
  75. */
  76. public function remove() {
  77. // unimplemented for guest user avatars
  78. }
  79. /**
  80. * Generates an avatar for the guest.
  81. *
  82. * @param int $size The desired image size.
  83. * @return InMemoryFile
  84. */
  85. public function getFile($size) {
  86. $avatar = $this->generateAvatar($this->userDisplayName, $size);
  87. return new InMemoryFile('avatar.png', $avatar);
  88. }
  89. /**
  90. * Updates the display name if changed.
  91. *
  92. * @param string $feature The changed feature
  93. * @param mixed $oldValue The previous value
  94. * @param mixed $newValue The new value
  95. * @return void
  96. */
  97. public function userChanged($feature, $oldValue, $newValue) {
  98. if ($feature === 'displayName') {
  99. $this->userDisplayName = $newValue;
  100. }
  101. }
  102. /**
  103. * Guests don't have custom avatars.
  104. *
  105. * @return bool
  106. */
  107. public function isCustomAvatar(): bool {
  108. return false;
  109. }
  110. }