Completed
Push — max-version-9.0-791d122c ( 791d12 )
by Thomas
26:14
created

ObjectCollection::listAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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\Db;
23
24
use Sabre\VObject\Component\VCalendar;
25
use Sabre\VObject\Component\VEvent;
26
use Sabre\VObject\Component\VJournal;
27
use Sabre\VObject\Component\VTodo;
28
use OCA\Calendar\IObjectCollection;
29
use OCA\Calendar\IObject;
30
use OCA\Calendar\ITimezone;
31
32
use DateTime;
33
34
class ObjectCollection extends Collection implements IObjectCollection {
35
36
	/**
37
	 * {@inheritDoc}
38
	 */
39 View Code Duplication
	public function inPeriod(DateTime $start, DateTime $end) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
40
		$objectsInPeriod = new ObjectCollection();
41
42
		foreach ($this->objects as $object) {
43
			if ($object->getRepeating() === true) {
44
				$objectsInPeriod->add($object);
45
			} else {
46
				if ($object->isInTimeRange($start, $end)) {
47
					$objectsInPeriod->add($object);
48
				}
49
			}
50
		}
51
52
		return $objectsInPeriod;
53
	}
54
55
56
	/**
57
	 * {@inheritDoc}
58
	 */
59 View Code Duplication
	public function expand(DateTime $start, DateTime $end) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
		$expandedObjects = new ObjectCollection();
61
62
		foreach ($this->objects as $object) {
63
			if ($object->getRepeating() === true) {
64
				$vobject = $object->getVObject();
65
				$vobject->expand($start, $end);
66
				$object->setVObject($vobject);
67
				$expandedObjects->add($object);
68
			}
69
		}
70
71
		return $expandedObjects;
72
	}
73
74
75
	/**
76
	 * {@inheritDoc}
77
	 */
78
	public function ownedBy($userId) {
79
		$objectsOwnedBy = new ObjectCollection();
80
81
		foreach ($this->objects as $object) {
82
			if ($object->getCalendar() instanceof Calendar &&
83
				$object->getCalendar()->getOwnerId() === $userId) {
84
				$objectsOwnedBy->add($object);
85
			}
86
		}
87
88
		return $objectsOwnedBy;
89
	}
90
91
92
	/**
93
	 * {@inheritDoc}
94
	 */
95
	public function ofType($type) {
96
		$objectsOfType = new ObjectCollection();
97
98
		foreach ($this->objects as $object) {
99
			if ($object->getType() & $type) {
100
				$objectsOfType->add($object);
101
			}
102
		}
103
104
		return $objectsOfType;
105
	}
106
107
108
	/**
109
	 * {@inheritDoc}
110
	 */
111
	public function addGlobalIds(array $idTable) {
112
		foreach ($this->objects as $object) {
113
			$uri = $object->getUri();
114
			if (isset($idTable[$uri])) {
115
				$object->setId($idTable[$uri]);
116
			} else {
117
				//TODO how to handle this case
118
			}
119
		}
120
	}
121
122
123
	/**
124
	 * {@inheritDoc}
125
	 */
126
	public function getVObject() {
127
		$vCalendar = new VCalendar();
128
129
		foreach($this->objects as &$object) {
130
			if (!($object instanceof IObject) && !($object instanceof ITimezone)) {
131
				continue;
132
			}
133
134
			$vObject = $object->getVObject();
135
			foreach($vObject->children() as $child) {
136
				if ($child instanceof VEvent ||
0 ignored issues
show
Bug introduced by
The class Sabre\VObject\Component\VEvent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
137
					$child instanceof VJournal ||
0 ignored issues
show
Bug introduced by
The class Sabre\VObject\Component\VJournal does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
138
					$child instanceof VTodo ||
0 ignored issues
show
Bug introduced by
The class Sabre\VObject\Component\VTodo does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
139
					$child->name === 'VTIMEZONE') {
140
					$vCalendar->add($child);
141
				}
142
			}
143
		}
144
145
		return $vCalendar;
146
	}
147
148
149
	/**
150
	 * {@inheritDoc}
151
	 */
152
	public function getVObjects() {
153
		$vObjects = array();
154
155
		foreach($this->objects as &$object) {
156
			if (!($object instanceof IObject) && !($object instanceof ITimezone)) {
157
				continue;
158
			}
159
160
			$vObjects[] = $object->getVObject();
161
		}
162
163
		return $vObjects;
164
	}
165
166
167
	/**
168
	 * {@inheritDoc}
169
	 */
170
	public function listAll($type=ObjectType::ALL) {
171
		$objects = $this->ofType($type);
172
173
		$list = [];
174
		foreach($objects as $object) {
175
			/** @var IObject $object */
176
			$list[] = $object->getUri();
177
		}
178
179
		return $list;
180
	}
181
}