Completed
Push — fix_broken_js_unit_test-d6ae96... ( d6ae96 )
by Thomas
21:39
created

Scanner   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 25
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 185
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
B scanObject() 0 25 5
B getRemoteAndDeleteIfNecessary() 0 24 5
A getCached() 0 11 3
A addToCache() 0 3 1
A updateCache() 0 3 1
A removeFromCache() 0 3 1
A scan() 0 18 4
A isCalendarsBackendValid() 0 3 2
1
<?php
2
/**
3
 * ownCloud - Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2014 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Cache\Object;
23
24
use OCA\Calendar\Backend\DoesNotExistException;
25
use OCA\Calendar\Backend\MultipleObjectsReturnedException;
26
use OCA\Calendar\Backend\TemporarilyNotAvailableException;
27
use OCA\Calendar\CorruptDataException;
28
use OCA\Calendar\IBackend;
29
use OCA\Calendar\ICalendar;
30
use OCA\Calendar\IObject;
31
32
use OCA\Calendar\Backend\IObjectAPI;
33
use OCA\Calendar\Backend as BackendUtils;
34
use OCP\ILogger;
35
36
use OCP\AppFramework\Db\DoesNotExistException as DoesNotExistMapperException;
37
use OCP\AppFramework\Db\MultipleObjectsReturnedException as MultipleObjectsReturnedMapperException;
38
39
class Scanner {
40
41
	/**
42
	 * @var ICalendar
43
	 */
44
	protected $calendar;
45
46
47
	/**
48
	 * @var \OCA\Calendar\Cache\Object\Cache
49
	 */
50
	protected $cache;
51
52
53
	/**
54
	 * @var \OCA\Calendar\Backend\IObjectAPI
55
	 */
56
	protected $objectAPI;
57
58
59
	/**
60
	 * @var ILogger
61
	 */
62
	protected $logger;
63
64
65
	/**
66
	 * @param ICalendar $calendar
67
	 */
68
	public function __construct(ICalendar $calendar) {
69
		$this->calendar = $calendar;
70
71
		$backend = $this->calendar->getBackend();
72
		if (!($backend instanceof IBackend)) {
73
			$identifier = implode('::', [
74
				$this->calendar->getUserId(),
75
				'?',
76
				$this->calendar->getPrivateUri(),
77
			]);
78
79
			$this->logger->error('Backend of calendar \'' . $identifier . '\' not found');
80
		} else {
81
			$this->cache = $backend->getObjectCache($calendar);
82
			try {
83
				$this->objectAPI = $backend->getObjectAPI($calendar);
84
		 	} catch (BackendUtils\Exception $ex) {
85
				//TODO
86
			}
87
		}
88
	}
89
90
91
	/**
92
	 * @param $objectUri
93
	 */
94
	public function scanObject($objectUri) {
95
		if (!$this->isCalendarsBackendValid()) {
96
			return;
97
		}
98
99
		$object = $this->getRemoteAndDeleteIfNecessary($objectUri);
100
		if (!$object) {
101
			return;
102
		}
103
104
		try {
105
			$cached = $this->getCached($objectUri);
106
		} catch (CorruptDataException $ex) {
107
			//TODO log $ex->getMessage();
108
109
			return;
110
		}
111
112
		if ($cached) {
113
			$object->setId($cached->getId());
114
			$this->updateCache($object);
115
		} else {
116
			$this->addToCache($object);
117
		}
118
	}
119
120
121
	/**
122
	 * @param $objectUri
123
	 * @return null|IObject
124
	 */
125
	protected function getRemoteAndDeleteIfNecessary($objectUri) {
126
		try {
127
			return $this->objectAPI->find($objectUri);
128
		} catch(DoesNotExistException $ex) {
129
			//$this->logger->debug($msg);
130
131
			$this->removeFromCache($objectUri);
132
			return null;
133
		} catch(MultipleObjectsReturnedException $ex) {
134
			//$this->logger->warn($msg);
135
136
			$this->removeFromCache($objectUri);
137
			return null;
138
		} catch(TemporarilyNotAvailableException $ex) {
139
			//$this->logger->debug($msg);
140
141
			return null;
142
		} catch(CorruptDataException $ex) {
143
			//$this->logger->debug($msg);
144
145
			$this->removeFromCache($objectUri);
146
			return null;
147
		}
148
	}
149
150
151
	/**
152
	 * @param $objectUri
153
	 * @return null|IObject
154
	 */
155
	protected function getCached($objectUri) {
156
		try {
157
			return $this->cache->find($objectUri);
158
		} catch(DoesNotExistMapperException $ex) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
159
			return null;
160
		} catch(MultipleObjectsReturnedMapperException $ex) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\Mult...bjectsReturnedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
161
			//$this->logger->warn($msg);
162
163
			return null;
164
		}
165
	}
166
167
168
	/**
169
	 * @param IObject $object
170
	 */
171
	protected function addToCache(IObject $object) {
172
		$this->cache->insert($object);
173
	}
174
175
176
	/**
177
	 * @param IObject $object
178
	 */
179
	protected function updateCache(IObject $object) {
180
		$this->cache->update($object);
181
	}
182
183
184
	/**
185
	 * @param string $objectUri
186
	 */
187
	protected function removeFromCache($objectUri) {
188
		$this->cache->deleteList([$objectUri]);
189
	}
190
191
192
	/**
193
	 * scan calendar for changed objects
194
	 */
195
	public function scan() {
196
		if (!$this->isCalendarsBackendValid()) {
197
			return;
198
		}
199
200
		$cachedList = $this->cache->listAll();
201
		try {
202
			$remoteList = $this->objectAPI->listAll();
203
		} catch(BackendUtils\Exception $ex) {
204
			return;
205
		}
206
207
		$list = array_merge($cachedList, $remoteList);
208
209
		foreach($list as $l) {
210
			$this->scanObject($l);
211
		}
212
	}
213
214
215
	/**
216
	 * @return boolean
217
	 */
218
	private function isCalendarsBackendValid() {
219
		return ($this->objectAPI instanceof IObjectAPI && $this->cache instanceof Cache);
220
	}
221
222
223
}