Completed
Push — master ( c20409...a4266f )
by Lukas
07:37
created

UUIDFixInsert::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
rs 9.4285
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
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
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\User_LDAP\Migration;
25
26
use OCA\User_LDAP\Mapping\GroupMapping;
27
use OCA\User_LDAP\Mapping\UserMapping;
28
use OCP\BackgroundJob\IJobList;
29
use OCP\IConfig;
30
use OCP\Migration\IOutput;
31
use OCP\Migration\IRepairStep;
32
33
class UUIDFixInsert implements IRepairStep {
34
35
	/** @var IConfig */
36
	protected $config;
37
38
	/** @var UserMapping */
39
	protected $userMapper;
40
41
	/** @var GroupMapping */
42
	protected $groupMapper;
43
44
	/** @var IJobList */
45
	protected $jobList;
46
47
	public function __construct(IConfig $config, UserMapping $userMapper, GroupMapping $groupMapper, IJobList $jobList) {
48
		$this->config = $config;
49
		$this->userMapper = $userMapper;
50
		$this->groupMapper = $groupMapper;
51
		$this->jobList = $jobList;
52
	}
53
54
	/**
55
	 * Returns the step's name
56
	 *
57
	 * @return string
58
	 * @since 9.1.0
59
	 */
60
	public function getName() {
61
		return 'Insert UUIDFix background job for user and group in batches';
62
	}
63
64
	/**
65
	 * Run repair step.
66
	 * Must throw exception on error.
67
	 *
68
	 * @param IOutput $output
69
	 * @throws \Exception in case of failure
70
	 * @since 9.1.0
71
	 */
72
	public function run(IOutput $output) {
73
		$installedVersion = $this->config->getAppValue('user_ldap', 'installed_version', '1.2.1');
74
		if(version_compare($installedVersion, '1.2.1') !== -1) {
75
			return;
76
		}
77
78
		foreach ([$this->userMapper, $this->groupMapper] as $mapper) {
79
			$offset = 0;
80
			$batchSize = 50;
81
			$jobClass = $mapper instanceof UserMapping ? UUIDFixUser::class : UUIDFixGroup::class;
82
			do {
83
				$retry = false;
84
				$records = $mapper->getList($offset, $batchSize);
85
				if(count($records) === 0){
86
					continue;
87
				}
88
				try {
89
					$this->jobList->add($jobClass, ['records' => $records]);
90
					$offset += $batchSize;
91
				} catch (\InvalidArgumentException $e) {
92
					if(strpos($e->getMessage(), 'Background job arguments can\'t exceed 4000') !== false) {
93
						$batchSize = intval(floor(count($records) * 0.8));
94
						$retry = true;
95
					}
96
				}
97
			} while (count($records) === $batchSize || $retry);
98
		}
99
100
	}
101
}
102