Completed
Push — master ( 0a370b...6164c7 )
by Maxence
02:08 queued 12s
created

Version0019Date20200702124412::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2019
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
declare(strict_types=1);
28
29
namespace OCA\Circles\Migration;
30
31
use Closure;
32
use daita\MySmallPhpTools\Traits\TStringTools;
33
use Doctrine\DBAL\Schema\SchemaException;
34
use OCA\Circles\Service\TimezoneService;
35
use OCP\DB\ISchemaWrapper;
36
use OCP\IDBConnection;
37
use OCP\Migration\IOutput;
38
use OCP\Migration\SimpleMigrationStep;
39
40
/**
41
 * Auto-generated migration step: Please modify to your needs!
42
 */
43
class Version0019Date20200702124412 extends SimpleMigrationStep {
44
45
46
	/** @var IDBConnection */
47
	private $connection;
48
49
	/** @var TimezoneService */
50
	private $timezoneService;
51
52
53
	/**
54
	 * @param IDBConnection $connection
55
	 * @param TimezoneService $timezoneService
56
	 */
57
	public function __construct(IDBConnection $connection, TimezoneService $timezoneService) {
58
		$this->connection = $connection;
59
		$this->timezoneService = $timezoneService;
60
	}
61
62
63
	/**
64
	 * @param IOutput $output
65
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
66
	 * @param array $options
67
	 *
68
	 * @return null|ISchemaWrapper
69
	 * @throws SchemaException
70
	 */
71
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
72
		/** @var ISchemaWrapper $schema */
73
		$schema = $schemaClosure();
74
75
		return $schema;
76
	}
77
78
79
	/**
80
	 * @param IOutput $output
81
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
82
	 * @param array $options
83
	 */
84
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
85
86
		/** @var ISchemaWrapper $schema */
87
		$schema = $schemaClosure();
88
89
		$this->migrateGroups($schema);
90
	}
91
92
93
	use TStringTools;
94
95
96
	/**
97
	 * @param ISchemaWrapper $schema
98
	 */
99
	private function migrateGroups(ISchemaWrapper $schema) {
100
		$qb = $this->connection->getQueryBuilder();
101
102
		$qb->select('*')
103
		   ->from('circle_groups');
104
105
		$result = $qb->execute();
106
		while ($row = $result->fetch()) {
107
108
			$current = $this->connection->getQueryBuilder();
109
			$current->select('*');
110
			$current->from('circle_members');
111
			$expr = $current->expr();
112
			$andX = $expr->andX();
113
			$andX->add($expr->eq('circle_id', $current->createNamedParameter($row['circle_id'])));
114
			$andX->add($expr->eq('user_id', $current->createNamedParameter($row['group_id'])));
115
			$andX->add($expr->eq('user_type', $current->createNamedParameter(2)));
116
			$andX->add($expr->eq('instance', $current->createNamedParameter('')));
117
			$current->where($andX);
118
119
			$cursor = $current->execute();
120
			$data = $cursor->fetch();
121
			$cursor->closeCursor();
122
			if ($data !== false) {
123
				continue;
124
			}
125
126
			$copy = $this->connection->getQueryBuilder();
127
			$memberId = $this->token(14);
128
129
			$copy->insert('circle_members')
130
				 ->setValue('circle_id', $copy->createNamedParameter($row['circle_id']))
131
				 ->setValue('user_id', $copy->createNamedParameter($row['group_id']))
132
				 ->setValue('level', $copy->createNamedParameter($row['level']))
133
				 ->setValue('member_id', $copy->createNamedParameter($memberId))
134
				 ->setValue('user_type', $copy->createNamedParameter(2))
135
				 ->setValue('status', $copy->createNamedParameter('Member'))
136
				 ->setValue('instance', $copy->createNamedParameter(''))
137
				 ->setValue('contact_id', $copy->createNamedParameter(''))
138
				 ->setValue('joined', $copy->createNamedParameter($this->timezoneService->getUTCDate()));
139
140
			$copy->execute();
141
		}
142
143
	}
144
145
146
}
147