1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - Calendar App |
4
|
|
|
* |
5
|
|
|
* @author Georg Ehrke |
6
|
|
|
* @copyright 2014 Georg Ehrke <[email protected]> |
7
|
|
|
* @author Maxime Corteel |
8
|
|
|
* @copyright 2014 Maxime Corteel <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This library is free software; you can redistribute it and/or |
11
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
12
|
|
|
* License as published by the Free Software Foundation; either |
13
|
|
|
* version 3 of the License, or any later version. |
14
|
|
|
* |
15
|
|
|
* This library is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public |
21
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
namespace OCA\Calendar\Backend\WebCal; |
25
|
|
|
|
26
|
|
|
use OCA\Calendar\Backend as BackendUtils; |
27
|
|
|
use OCA\Calendar\BusinessLayer; |
28
|
|
|
use OCA\Calendar\CorruptDataException; |
29
|
|
|
use OCA\Calendar\Db\ObjectFactory; |
30
|
|
|
use OCA\Calendar\ICalendar; |
31
|
|
|
use OCA\Calendar\IObject; |
32
|
|
|
use OCA\Calendar\IObjectCollection; |
33
|
|
|
use OCA\Calendar\ISubscription; |
34
|
|
|
use OCP\ICacheFactory; |
35
|
|
|
use OCP\IL10N; |
36
|
|
|
use Sabre\VObject\ParseException; |
37
|
|
|
|
38
|
|
|
use OCA\Calendar\Db\ObjectType; |
39
|
|
|
|
40
|
|
|
class Object extends WebCal implements BackendUtils\IObjectAPI { |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ICalendar |
44
|
|
|
*/ |
45
|
|
|
private $calendar; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ISubscription |
50
|
|
|
*/ |
51
|
|
|
private $subscription; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ObjectFactory |
56
|
|
|
*/ |
57
|
|
|
private $factory; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var IObjectCollection |
62
|
|
|
*/ |
63
|
|
|
private $objects; |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param BusinessLayer\Subscription $subscriptions |
68
|
|
|
* @param IL10N $l10n |
69
|
|
|
* @param ICacheFactory $cacheFactory |
70
|
|
|
* @param ICalendar $calendar |
71
|
|
|
* @param ObjectFactory $factory |
72
|
|
|
* @throws BackendUtils\DoesNotExistException if no corresponding subscription was found |
73
|
|
|
*/ |
74
|
|
|
public function __construct(BusinessLayer\Subscription $subscriptions, IL10N $l10n, ICacheFactory $cacheFactory, |
75
|
|
|
ICalendar $calendar, ObjectFactory $factory) { |
76
|
|
|
parent::__construct($subscriptions, $l10n, $cacheFactory); |
77
|
|
|
$this->calendar = $calendar; |
78
|
|
|
$this->factory = $factory; |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$calendar = $this->calendar; |
82
|
|
|
$this->subscription = $this->subscriptions->findByType( |
83
|
|
|
$calendar->getPrivateUri(), self::IDENTIFIER, $calendar->getUserId()); |
84
|
|
|
} catch(BusinessLayer\Exception $ex) { |
85
|
|
|
throw new BackendUtils\DoesNotExistException($ex->getMessage()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function cache() { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function find($objectURI, $type=ObjectType::ALL) { |
102
|
|
View Code Duplication |
if (!($this->objects instanceof IObjectCollection)) { |
|
|
|
|
103
|
|
|
$this->findAll(); |
104
|
|
|
|
105
|
|
|
if (!($this->objects instanceof IObjectCollection)) { |
106
|
|
|
throw new BackendUtils\DoesNotExistException('Object not found'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
foreach($this->objects as $object) { |
111
|
|
|
/** @var IObject $object */ |
112
|
|
|
if ($object->getUri() === $objectURI) { |
113
|
|
|
if ($object->getType() & $type) { |
114
|
|
|
return $object; |
115
|
|
|
} else { |
116
|
|
|
throw new BackendUtils\DoesNotExistException('Object exists, but is of wrong type'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
throw new BackendUtils\DoesNotExistException('Object not found'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
|
|
*/ |
128
|
|
|
public function findAll($type=ObjectType::ALL, $limit=null, $offset=null) { |
129
|
|
|
if ($this->objects instanceof IObjectCollection) { |
130
|
|
|
return $this->objects->ofType($type); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$data = $this->getData($this->subscription); |
134
|
|
|
try { |
135
|
|
|
$this->objects = $this->factory->createCollectionFromData($data, ObjectFactory::FORMAT_ICAL); |
136
|
|
|
|
137
|
|
|
foreach ($this->objects as $object) { |
|
|
|
|
138
|
|
|
/** @var IObject $object */ |
139
|
|
|
$object->setCalendar($this->calendar); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->objects->ofType($type); |
143
|
|
|
} catch(ParseException $ex) { |
|
|
|
|
144
|
|
|
throw new CorruptDataException('CalendarManager-data is not valid!'); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
|
|
public function listAll($type=ObjectType::ALL) { |
153
|
|
View Code Duplication |
if (!($this->objects instanceof IObjectCollection)) { |
|
|
|
|
154
|
|
|
$this->findAll(); |
155
|
|
|
|
156
|
|
|
if (!($this->objects instanceof IObjectCollection)) { |
157
|
|
|
return []; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->objects->listAll($type); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritDoc} |
167
|
|
|
*/ |
168
|
|
|
public function hasUpdated(IObject $object) { |
169
|
|
|
$newObject = $this->find($object->getUri()); |
170
|
|
|
|
171
|
|
|
return ($newObject->getEtag(true) !== $object->getEtag(true)); |
172
|
|
|
} |
173
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.