Completed
Pull Request — master (#517)
by Maxence
02:08
created

CleanService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Service;
31
32
33
use OCA\Circles\Db\CirclesRequest;
34
use OCA\Circles\Db\CoreRequestBuilder;
35
use OCA\Circles\Db\MembersRequest;
36
use OCA\Circles\Db\SharesRequest;
37
use OCA\Circles\Exceptions\CircleDoesNotExistException;
38
use OCA\Circles\Model\Circle;
39
use OCP\IDBConnection;
40
41
/**
42
 * Class CleanService
43
 *
44
 * @package OCA\Circles\Service
45
 */
46
class CleanService {
47
48
49
	/** @var IDBConnection */
50
	private $dbConnection;
51
52
	/** @var CirclesRequest */
53
	private $circlesRequest;
54
55
	/** @var MembersRequest */
56
	private $membersRequest;
57
58
	/** @var SharesRequest */
59
	private $sharesRequest;
60
61
62
	/**
63
	 * Clean constructor.
64
	 *
65
	 * @param IDBConnection $connection
66
	 * @param CirclesRequest $circlesRequest
67
	 * @param MembersRequest $membersRequest
68
	 * @param SharesRequest $sharesRequest
69
	 */
70
	public function __construct(
71
		IDBConnection $connection, CirclesRequest $circlesRequest, MembersRequest $membersRequest,
72
		SharesRequest $sharesRequest
73
	) {
74
		$this->dbConnection = $connection;
75
		$this->circlesRequest = $circlesRequest;
76
		$this->membersRequest = $membersRequest;
77
		$this->sharesRequest = $sharesRequest;
78
	}
79
80
81
	public function clean() {
82
		$this->fixUserType();
83
		$this->removeCirclesWithNoOwner();
84
		$this->removeMembersWithNoCircles();
85
		$this->removeDeprecatedShares();
86
	}
87
88
89
	public 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
		$qb->execute();
99
	}
100
101
102
	public function removeCirclesWithNoOwner() {
103
		$circles = $this->circlesRequest->forceGetCircles();
104
105
		foreach ($circles as $circle) {
106
			if ($circle->getOwner()
107
					   ->getUserId() === null) {
108
				$this->circlesRequest->destroyCircle($circle->getUniqueId());
109
			}
110
		}
111
	}
112
113
114
	public function removeMembersWithNoCircles() {
115
		$members = $this->membersRequest->forceGetAllMembers();
116
117
		foreach ($members as $member) {
118
			try {
119
				$this->circlesRequest->forceGetCircle($member->getCircleId());
120
			} catch (CircleDoesNotExistException $e) {
121
				$this->membersRequest->removeMember($member);
122
			}
123
		}
124
	}
125
126
127
	public function removeDeprecatedShares() {
128
		$circles = array_map(
129
			function(Circle $circle) {
130
				return $circle->getUniqueId();
131
			}, $this->circlesRequest->forceGetCircles()
132
		);
133
134
		$shares = array_unique(
135
			array_map(
136
				function($share) {
137
					return $share['share_with'];
138
				}, $this->sharesRequest->getShares()
139
			)
140
		);
141
142
		foreach ($shares as $share) {
143
			if (!in_array($share, $circles)) {
144
				$this->sharesRequest->removeSharesToCircleId($share);
145
			}
146
		}
147
	}
148
149
}
150
151
152