Completed
Push — master ( 6c2c12...eeb0cf )
by Morris
12:42
created

CalendarObject::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 * @copyright Copyright (c) 2017, Georg Ehrke
5
 *
6
 * @author Georg Ehrke <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program 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 License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
26
namespace OCA\DAV\CalDAV;
27
28
29
use Sabre\VObject\Component;
30
use Sabre\VObject\Property;
31
use Sabre\VObject\Reader;
32
33
class CalendarObject extends \Sabre\CalDAV\CalendarObject {
34
35
	/**
36
	 * CalendarObject constructor.
37
	 *
38
	 * @param CalDavBackend $caldavBackend
39
	 * @param array $calendarInfo
40
	 * @param array $objectData
41
	 */
42
	public function __construct(CalDavBackend $caldavBackend, array $calendarInfo,
43
								array $objectData) {
44
		parent::__construct($caldavBackend, $calendarInfo, $objectData);
45
46
		if ($this->isShared()) {
47
			unset($this->objectData['size']);
48
		}
49
	}
50
51
	/**
52
	 * @inheritdoc
53
	 */
54
	function get() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
		$data = parent::get();
56
57
		if (!$this->isShared()) {
58
			return $data;
59
		}
60
61
		$vObject = Reader::read($data);
62
63
		// remove VAlarms if calendar is shared read-only
64
		if (!$this->canWrite()) {
65
			$this->removeVAlarms($vObject);
66
		}
67
68
		// shows as busy if event is declared confidential
69
		if ($this->objectData['classification'] === CalDavBackend::CLASSIFICATION_CONFIDENTIAL) {
70
			$this->createConfidentialObject($vObject);
71
		}
72
73
		return $vObject->serialize();
74
	}
75
76 View Code Duplication
	protected function isShared() {
77
		if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
78
			return false;
79
		}
80
81
		return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
82
	}
83
84
	/**
85
	 * @param Component\VCalendar $vObject
86
	 * @return void
87
	 */
88
	private static function createConfidentialObject(Component\VCalendar $vObject) {
89
		/** @var Component $vElement */
90
		$vElement = null;
91
		if(isset($vObject->VEVENT)) {
92
			$vElement = $vObject->VEVENT;
93
		}
94
		if(isset($vObject->VJOURNAL)) {
95
			$vElement = $vObject->VJOURNAL;
96
		}
97
		if(isset($vObject->VTODO)) {
98
			$vElement = $vObject->VTODO;
99
		}
100
		if(!is_null($vElement)) {
101
			foreach ($vElement->children() as &$property) {
0 ignored issues
show
Bug introduced by
The expression $vElement->children() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
102
				/** @var Property $property */
103
				switch($property->name) {
104
					case 'CREATED':
105
					case 'DTSTART':
106
					case 'RRULE':
107
					case 'DURATION':
108
					case 'DTEND':
109
					case 'CLASS':
110
					case 'UID':
111
						break;
112
					case 'SUMMARY':
113
						$property->setValue('Busy');
114
						break;
115
					default:
116
						$vElement->__unset($property->name);
117
						unset($property);
118
						break;
119
				}
120
			}
121
		}
122
	}
123
124
	/**
125
	 * @param Component\VCalendar $vObject
126
	 * @return void
127
	 */
128
	private function removeVAlarms(Component\VCalendar $vObject) {
129
		$subcomponents = $vObject->getComponents();
130
131
		foreach($subcomponents as $subcomponent) {
132
			unset($subcomponent->VALARM);
133
		}
134
	}
135
136
	/**
137
	 * @return bool
138
	 */
139
	private function canWrite() {
140
		if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
141
			return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
142
		}
143
		return true;
144
	}
145
}
146