Passed
Push — master ( b69b17...32a6f4 )
by Morris
11:33
created

GuestAvatar::getDisplayName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @copyright Copyright (c) 2018, Michael Weimann <[email protected]>
6
 *
7
 * @author Michael Weimann <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
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, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 */
23
24
namespace OC\Avatar;
25
26
use OCP\Files\SimpleFS\InMemoryFile;
27
use OCP\ILogger;
28
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
	 * @var string
37
	 */
38
	private $userDisplayName;
39
40
	/**
41
	 * GuestAvatar constructor.
42
	 *
43
	 * @param string $userDisplayName The guest user display name
44
	 * @param ILogger $logger The logger
45
	 */
46
	public function __construct(string $userDisplayName, ILogger $logger) {
47
		parent::__construct($logger);
48
		$this->userDisplayName = $userDisplayName;
49
	}
50
51
	/**
52
	 * Tests if the user has an avatar.
53
	 *
54
	 * @return true Guests always have an avatar.
55
	 */
56
	public function exists() {
57
		return true;
58
	}
59
60
	/**
61
	 * Returns the guest user display name.
62
	 *
63
	 * @return string
64
	 */
65
	public function getDisplayName(): string {
66
		return $this->userDisplayName;
67
	}
68
69
	/**
70
	 * Setting avatars isn't implemented for guests.
71
	 *
72
	 * @param \OCP\IImage|resource|string $data
73
	 * @return void
74
	 */
75
	public function set($data) {
76
		// unimplemented for guest user avatars
77
	}
78
79
	/**
80
	 * Removing avatars isn't implemented for guests.
81
	 */
82
	public function remove() {
83
		// unimplemented for guest user avatars
84
	}
85
86
	/**
87
	 * Generates an avatar for the guest.
88
	 *
89
	 * @param int $size The desired image size.
90
	 * @return InMemoryFile
91
	 */
92
	public function getFile($size) {
93
		$avatar = $this->generateAvatar($this->userDisplayName, $size);
94
		return new InMemoryFile('avatar.png', $avatar);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new OCP\Files\Sim...('avatar.png', $avatar) returns the type OCP\Files\SimpleFS\InMemoryFile which is incompatible with the return type mandated by OCP\IAvatar::getFile() of OCP\Files\File.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
95
	}
96
97
	/**
98
	 * Updates the display name if changed.
99
	 *
100
	 * @param string $feature The changed feature
101
	 * @param mixed $oldValue The previous value
102
	 * @param mixed $newValue The new value
103
	 * @return void
104
	 */
105
	public function userChanged($feature, $oldValue, $newValue) {
106
		if ($feature === 'displayName') {
107
			$this->userDisplayName = $newValue;
108
		}
109
	}
110
111
	/**
112
	 * Guests don't have custom avatars.
113
	 *
114
	 * @return bool
115
	 */
116
	public function isCustomAvatar(): bool {
117
		return false;
118
	}
119
}
120