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

Object::find()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 22
Code Lines 12

Duplication

Lines 7
Ratio 31.82 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 7
loc 22
rs 8.6737
cc 6
eloc 12
nc 9
nop 2
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The expression $this->objects of type object<OCA\Calendar\Db\ObjectCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
138
				/** @var IObject $object */
139
				$object->setCalendar($this->calendar);
140
			}
141
142
			return $this->objects->ofType($type);
143
		} catch(ParseException $ex) {
0 ignored issues
show
Bug introduced by
The class Sabre\VObject\ParseException 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...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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
}