Completed
Pull Request — master (#362)
by Maxence
02:21
created

GlobalSync::syncCircles()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.2728
c 0
b 0
f 0
cc 5
nc 6
nop 0
1
<?php declare(strict_types=1);
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\Cron;
31
32
33
use OC\BackgroundJob\TimedJob;
34
use OCA\Circles\AppInfo\Application;
35
use OCA\Circles\Db\CirclesRequest;
36
use OCA\Circles\Db\MembersRequest;
37
use OCA\Circles\Exceptions\GSStatusException;
38
use OCA\Circles\Model\Circle;
39
use OCA\Circles\Service\ConfigService;
40
use OCA\Circles\Service\GSUpstreamService;
41
use OCA\Circles\Service\MiscService;
42
use OCP\AppFramework\QueryException;
43
44
45
/**
46
 * Class GlobalSync
47
 *
48
 * @package OCA\Cicles\Cron
49
 */
50
class GlobalSync extends TimedJob {
51
52
53
	/** @var MembersRequest */
54
	private $membersRequest;
55
56
	/** @var CirclesRequest */
57
	private $circlesRequest;
58
59
	/** @var GSUpstreamService */
60
	private $gsUpstreamService;
61
62
	/** @var ConfigService */
63
	private $configService;
64
65
	/** @var MiscService */
66
	private $miscService;
67
68
69
	/**
70
	 * Cache constructor.
71
	 */
72
	public function __construct() {
73
		$this->setInterval(1);
74
	}
75
76
77
	/**
78
	 * @param mixed $argument
79
	 *
80
	 * @throws QueryException
81
	 */
82
	protected function run($argument) {
83
		$app = new Application();
84
		$c = $app->getContainer();
85
86
		$this->circlesRequest = $c->query(CirclesRequest::class);
87
		$this->membersRequest = $c->query(MembersRequest::class);
88
		$this->gsUpstreamService = $c->query(GSUpstreamService::class);
89
		$this->configService = $c->query(ConfigService::class);
90
		$this->miscService = $c->query(MiscService::class);
91
92
		try {
93
			if (!$this->configService->getGSStatus(ConfigService::GS_ENABLED)) {
94
				return;
95
			}
96
		} catch (GSStatusException $e) {
97
			return;
98
		}
99
100
		$this->syncCircles();
101
		$this->removeDeprecatedCircles();
102
	}
103
104
105
	private function syncCircles() {
106
		$circles = $this->circlesRequest->forceGetCircles();
107
		$sync = [];
108
		foreach ($circles as $circle) {
109
			if ($circle->getOwner()
110
					   ->getInstance() !== ''
111
				|| $circle->getType() === Circle::CIRCLES_PERSONAL) {
112
				continue;
113
			}
114
115
			$members = $this->membersRequest->forceGetMembers($circle->getUniqueId());
116
			$circle->setMembers($members);
117
118
			$sync[] = $circle;
119
		}
120
121
		try {
122
			$this->gsUpstreamService->syncCircles($sync);
123
		} catch (GSStatusException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
124
		}
125
	}
126
127
128
	/**
129
	 *
130
	 */
131
	private function removeDeprecatedCircles(): void {
132
		$knownCircles = $this->circlesRequest->forceGetCircles();
133
134
		foreach ($knownCircles as $knownItem) {
135
			if ($knownItem->getOwner()
136
						  ->getInstance() === '') {
137
				continue;
138
			}
139
140
			try {
141
				$this->checkCircle($knownItem);
142
			} catch (GSStatusException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
143
			}
144
		}
145
	}
146
147
148
	/**
149
	 * @param Circle $circle
150
	 *
151
	 * @throws GSStatusException
152
	 */
153
	private function checkCircle(Circle $circle): void {
154
		$status = $this->gsUpstreamService->confirmCircleStatus($circle);
155
156
		if (!$status) {
157
			$this->circlesRequest->destroyCircle($circle->getUniqueId());
158
			$this->membersRequest->removeAllFromCircle($circle->getUniqueId());
159
		}
160
	}
161
162
}
163