Completed
Push — master ( 7d1f36...e920e2 )
by Morris
12:30
created

CopyEntriesFromOldTable::run()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 27
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 41
rs 8.5806
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[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\TwoFactorBackupCodes\Migration;
25
26
use OCP\DB\QueryBuilder\IQueryBuilder;
27
use OCP\IConfig;
28
use OCP\IDBConnection;
29
use OCP\Migration\IOutput;
30
use OCP\Migration\IRepairStep;
31
32
class CopyEntriesFromOldTable implements IRepairStep {
33
34
	/** @var IDBConnection */
35
	protected $connection;
36
37
	/** @var IConfig */
38
	protected $config;
39
40
	/**
41
	 * @param IDBConnection $connection
42
	 * @param IConfig $config
43
	 */
44
	public function __construct(IDBConnection $connection, IConfig $config) {
45
		$this->connection = $connection;
46
		$this->config = $config;
47
	}
48
49
	/**
50
	 * Returns the step's name
51
	 *
52
	 * @return string
53
	 * @since 9.1.0
54
	 */
55
	public function getName() {
56
		return 'Copy twofactor backup codes from legacy table';
57
	}
58
59
	/**
60
	 * Run repair step.
61
	 * Must throw exception on error.
62
	 *
63
	 * @since 9.1.0
64
	 * @param IOutput $output
65
	 * @throws \Exception in case of failure
66
	 */
67
	public function run(IOutput $output) {
68
		$version = $this->config->getAppValue('twofactor_backupcodes', 'installed_version', '0.0.0');
69
		if (version_compare($version, '1.1.1', '>=')) {
70
			return;
71
		}
72
73
		if (!$this->connection->tableExists('twofactor_backup_codes')) {
74
			// Legacy table does not exist
75
			return;
76
		}
77
78
		$insert = $this->connection->getQueryBuilder();
79
		$insert->insert('twofactor_backupcodes')
80
			->values([
81
				// Inserting with id might fail: 'id' => $insert->createParameter('id'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
				'user_id' => $insert->createParameter('user_id'),
83
				'code' => $insert->createParameter('code'),
84
				'used' => $insert->createParameter('used'),
85
			]);
86
87
		$query = $this->connection->getQueryBuilder();
88
		$query->select('*')
89
			->from('twofactor_backup_codes')
90
			->orderBy('id', 'ASC');
91
		$result = $query->execute();
92
93
		$output->startProgress();
94
		while ($row = $result->fetch()) {
95
			$output->advance();
96
97
			$insert
98
				// Inserting with id might fail: ->setParameter('id', $row['id'], IQueryBuilder::PARAM_INT)
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
				->setParameter('user_id', $row['user_id'], IQueryBuilder::PARAM_STR)
100
				->setParameter('code', $row['code'], IQueryBuilder::PARAM_STR)
101
				->setParameter('used', $row['used'], IQueryBuilder::PARAM_INT)
102
				->execute();
103
		}
104
		$output->finishProgress();
105
106
		$this->connection->dropTable('twofactor_backup_codes');
107
	}
108
}
109