Completed
Push — master ( 994b8c...0f8bf0 )
by Thomas
13:59
created

MoveAvatarOutsideHome   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 30.48 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 32
loc 105
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
A getName() 0 3 1
A moveAvatars() 0 23 3
A run() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author Vincent Petry <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
namespace OC\Repair;
22
23
use OCP\Files\IRootFolder;
24
use OCP\IDBConnection;
25
use OCP\IL10N;
26
use OCP\ILogger;
27
use OCP\IUserManager;
28
use OCP\Migration\IOutput;
29
use OCP\Migration\IRepairStep;
30
use OCP\IUser;
31
use OC\Avatar;
32
use OCP\IConfig;
33
use OCP\IAvatarManager;
34
use OCP\Files\NotFoundException;
35
36
/**
37
 * Move avatars outside of their homes to the new location
38
 *
39
 * @package OC\Repair
40
 */
41
class MoveAvatarOutsideHome implements IRepairStep {
42
	/** @var \OCP\IConfig */
43
	protected $config;
44
45
	/** @var IDBConnection */
46
	private $connection;
47
48
	/** @var IUserManager */
49
	private $userManager;
50
51
	/** @var IAvatarManager */
52
	private $avatarManager;
53
54
	/** @var IRootFolder */
55
	private $rootFolder;
56
57
	/** @var \OCP\ILogger */
58
	private $logger;
59
60
	/** @var \OCP\IL10N */
61
	private $l;
62
63
	/**
64
	 * @param IConfig $config config
65
	 * @param IDBConnection $connection database connection
66
	 * @param IUserManager $userManager user manager
67
	 * @param IAvatarManager $avatarManager
68
	 * @param IRootFolder $rootFolder
69
	 * @param IL10N $l10n
70
	 * @param ILogger $logger
71
	 */
72 View Code Duplication
	public function __construct(
73
		IConfig $config,
74
		IDBConnection $connection,
75
		IUserManager $userManager,
76
		IAvatarManager $avatarManager,
77
		IRootFolder $rootFolder,
78
		IL10N $l10n,
79
		ILogger $logger
80
	) {
81
		$this->config = $config;
82
		$this->connection = $connection;
83
		$this->userManager = $userManager;
84
		$this->avatarManager = $avatarManager;
85
		$this->rootFolder = $rootFolder;
86
		$this->l = $l10n;
87
		$this->logger = $logger;
88
	}
89
90
	/**
91
	 * @return string
92
	 */
93
	public function getName() {
94
		return 'Move user avatars outside the homes to the new location';
95
	}
96
97
	/**
98
	 * Move avatars outside of their homes
99
	 *
100
	 * @param IOutput $out
101
	 * @param IUser $user
102
	 */
103
	private function moveAvatars(IOutput $out, IUser $user) {
0 ignored issues
show
Unused Code introduced by
The parameter $out is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
		$userId = $user->getUID();
105
106
		\OC\Files\Filesystem::initMountPoints($userId);
107
108
		// call get instead of getUserFolder to avoid needless skeleton copy
109
		try {
110
			$oldAvatarUserFolder = $this->rootFolder->get('/' . $userId);
111
			$oldAvatar = new Avatar($oldAvatarUserFolder, $this->l, $user, $this->logger);
0 ignored issues
show
Compatibility introduced by
$oldAvatarUserFolder of type object<OCP\Files\Node> is not a sub-type of object<OCP\Files\Folder>. It seems like you assume a child interface of the interface OCP\Files\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$user of type object<OCP\IUser> is not a sub-type of object<OC\User\User>. It seems like you assume a concrete implementation of the interface OCP\IUser to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
112
			if ($oldAvatar->exists()) {
113
				$newAvatarsUserFolder = $this->avatarManager->getAvatarFolder($userId);
0 ignored issues
show
Bug introduced by
The method getAvatarFolder() does not exist on OCP\IAvatarManager. Did you maybe mean getAvatar()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
114
115
				// get original file
116
				$oldAvatarFile = $oldAvatar->getFile(-1);
117
				$oldAvatarFile->move($newAvatarsUserFolder->getPath() . '/' . $oldAvatarFile->getName());
118
				$oldAvatar->remove();
119
			}
120
		} catch (NotFoundException $e) {
121
			// not all users have a home, ignore
122
		}
123
124
		\OC_Util::tearDownFS();
125
	}
126
127
	/**
128
	 * @param IOutput $output
129
	 */
130 View Code Duplication
	public function run(IOutput $output) {
131
		$ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
132
		if (version_compare($ocVersionFromBeforeUpdate, '9.2.0.2', '<')) {
133
			$function = function(IUser $user) use ($output) {
134
				$this->moveAvatars($output, $user);
135
				$output->advance();
136
			};
137
138
			$output->startProgress($this->userManager->countSeenUsers());
139
140
			$this->userManager->callForSeenUsers($function);
141
142
			$output->finishProgress();
143
		}
144
	}
145
}
146
147