Completed
Push — master ( 31d56c...de2ce6 )
by Maxence
02:07
created

Clean::removeMembersWithNoCircles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
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 2017
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
namespace OCA\Circles\Command;
28
29
use Exception;
30
use OC\Core\Command\Base;
31
use OCA\Circles\Db\CirclesRequest;
32
use OCA\Circles\Db\CoreRequestBuilder;
33
use OCA\Circles\Db\MembersRequest;
34
use OCA\Circles\Exceptions\CircleDoesNotExistException;
35
use OCP\IDBConnection;
36
use Symfony\Component\Console\Input\InputInterface;
37
use Symfony\Component\Console\Output\OutputInterface;
38
39
40
class Clean extends Base {
41
42
	/** @var IDBConnection */
43
	private $dbConnection;
44
45
	/** @var CirclesRequest */
46
	private $circlesRequest;
47
48
	/** @var MembersRequest */
49
	private $membersRequest;
50
51
52
	/**
53
	 * Clean constructor.
54
	 *
55
	 * @param IDBConnection $connection
56
	 * @param CirclesRequest $circlesRequest
57
	 * @param MembersRequest $membersRequest
58
	 */
59
	public function __construct(
60
		IDBConnection $connection, CirclesRequest $circlesRequest, MembersRequest $membersRequest
61
	) {
62
		parent::__construct();
63
		$this->dbConnection = $connection;
64
		$this->circlesRequest = $circlesRequest;
65
		$this->membersRequest = $membersRequest;
66
67
	}
68
69
	protected function configure() {
70
		parent::configure();
71
		$this->setName('circles:clean')
72
			 ->setDescription('remove all extra data from database');
73
	}
74
75
	protected function execute(InputInterface $input, OutputInterface $output) {
76
77
		try {
78
			$this->fixUserType();
79
			$this->removeCirclesWithNoOwner();
80
			$this->removeMembersWithNoCircles();
81
82
			$output->writeln('done');
83
		} catch (Exception $e) {
84
			$output->writeln($e->getMessage());
85
		}
86
	}
87
88
89
	private function fixUserType() {
90
		$qb = $this->dbConnection->getQueryBuilder();
91
		$qb->update(CoreRequestBuilder::TABLE_MEMBERS)
92
		   ->set('user_type', $qb->createNamedParameter(1))
93
		   ->where(
94
			   $qb->expr()
95
				  ->eq('user_type', $qb->createNamedParameter(0))
96
		   );
97
98
		return $qb->execute();
99
100
101
	}
102
103
	private function removeCirclesWithNoOwner() {
104
105
		$circles = $this->circlesRequest->forceGetCircles();
106
107
		foreach ($circles as $circle) {
108
			if ($circle->getOwner()
109
					   ->getUserId() === null) {
110
				$this->circlesRequest->destroyCircle($circle->getUniqueId());
111
			}
112
		}
113
	}
114
115
	private function removeMembersWithNoCircles() {
116
117
		$members = $this->membersRequest->forceGetAllMembers();
118
119
		foreach ($members as $member) {
120
			try {
121
				$this->circlesRequest->forceGetCircle($member->getCircleId());
122
123
			} catch (CircleDoesNotExistException $e) {
124
				$this->membersRequest->removeMember($member);
125
			}
126
		}
127
128
	}
129
}
130
131
132
133