Completed
Push — master ( b5d31e...39468f )
by Lukas
13:10
created

StorageMigrator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 15.79 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 15
loc 95
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B migrate() 0 20 5
A __construct() 15 15 1
A migrateUser() 0 13 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
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Vincent Petry <[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
25
namespace OCA\Files_External\Migration;
26
27
use OCA\Files_External\Service\BackendService;
28
use OCA\Files_External\Service\DBConfigService;
29
use OCA\Files_External\Service\GlobalLegacyStoragesService;
30
use OCA\Files_External\Service\GlobalStoragesService;
31
use OCA\Files_External\Service\LegacyStoragesService;
32
use OCA\Files_External\Service\StoragesService;
33
use OCA\Files_External\Service\UserLegacyStoragesService;
34
use OCA\Files_External\Service\UserStoragesService;
35
use OCP\Files\Config\IUserMountCache;
36
use OCP\IConfig;
37
use OCP\IDBConnection;
38
use OCP\ILogger;
39
use OCP\IUser;
40
41
/**
42
 * Migrate mount config from mount.json to the database
43
 */
44
class StorageMigrator {
45
	/**
46
	 * @var BackendService
47
	 */
48
	private $backendService;
49
50
	/**
51
	 * @var DBConfigService
52
	 */
53
	private $dbConfig;
54
55
	/**
56
	 * @var IConfig
57
	 */
58
	private $config;
59
60
	/**
61
	 * @var IDBConnection
62
	 */
63
	private $connection;
64
65
	/**
66
	 * @var ILogger
67
	 */
68
	private $logger;
69
70
	/** @var IUserMountCache  */
71
	private $userMountCache;
72
73
	/**
74
	 * StorageMigrator constructor.
75
	 *
76
	 * @param BackendService $backendService
77
	 * @param DBConfigService $dbConfig
78
	 * @param IConfig $config
79
	 * @param IDBConnection $connection
80
	 * @param ILogger $logger
81
	 * @param IUserMountCache $userMountCache
82
	 */
83 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
		BackendService $backendService,
85
		DBConfigService $dbConfig,
86
		IConfig $config,
87
		IDBConnection $connection,
88
		ILogger $logger,
89
		IUserMountCache $userMountCache
90
	) {
91
		$this->backendService = $backendService;
92
		$this->dbConfig = $dbConfig;
93
		$this->config = $config;
94
		$this->connection = $connection;
95
		$this->logger = $logger;
96
		$this->userMountCache = $userMountCache;
97
	}
98
99
	private function migrate(LegacyStoragesService $legacyService, StoragesService $storageService) {
100
		$existingStorage = $legacyService->getAllStorages();
101
102
		$this->connection->beginTransaction();
103
		try {
104
			foreach ($existingStorage as $storage) {
105
				$mountOptions = $storage->getMountOptions();
106
				if (!empty($mountOptions) && !isset($mountOptions['enable_sharing'])) {
107
					// existing mounts must have sharing enabled by default to avoid surprises
108
					$mountOptions['enable_sharing'] = true;
109
					$storage->setMountOptions($mountOptions);
110
				}
111
				$storageService->addStorage($storage);
112
			}
113
			$this->connection->commit();
114
		} catch (\Exception $e) {
115
			$this->logger->logException($e);
116
			$this->connection->rollBack();
117
		}
118
	}
119
120
	/**
121
	 * Migrate personal storages configured by the current user
122
	 *
123
	 * @param IUser $user
124
	 */
125
	public function migrateUser(IUser $user) {
126
		$dummySession = new DummyUserSession();
127
		$dummySession->setUser($user);
128
		$userId = $user->getUID();
129
		$userVersion = $this->config->getUserValue($userId, 'files_external', 'config_version', '0.0.0');
130
		if (version_compare($userVersion, '0.5.0', '<')) {
131
			$this->config->setUserValue($userId, 'files_external', 'config_version', '0.5.0');
132
			$legacyService = new UserLegacyStoragesService($this->backendService, $dummySession);
133
			$storageService = new UserStoragesService($this->backendService, $this->dbConfig, $dummySession, $this->userMountCache);
134
135
			$this->migrate($legacyService, $storageService);
136
		}
137
	}
138
}
139