Completed
Pull Request — master (#582)
by
unknown
123:49 queued 122:03
created

DataHelper::parseParameters()   D

Complexity

Conditions 19
Paths 25

Size

Total Lines 46
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 19.1641

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 24
cts 26
cp 0.9231
rs 4.9008
c 0
b 0
f 0
cc 19
eloc 20
nc 25
nop 1
crap 19.1641

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Activity;
23
24
use OC\Group\Manager;
25
use OCA\Activity\Parameter\Factory;
26
use OCA\Activity\Parameter\IParameter;
27
use OCA\Activity\Parameter\Collection;
28
use OCP\Activity\IEvent;
29
use OCP\Activity\IManager;
30
use OCP\IL10N;
31
use OCP\L10N\IFactory;
32
33
class DataHelper {
34
	/** @var \OCP\Activity\IManager */
35
	protected $activityManager;
36
37
	/** @var \OCA\Activity\Parameter\Factory */
38
	protected $parameterFactory;
39
40
	/** @var IFactory */
41
	protected $l10Nfactory;
42
43
	/** @var IL10N */
44
	protected $l;
45
46
	/** @var Manager  */
47
	protected $groupManager;
48
49
	/**
50
	 * DataHelper constructor.
51
	 *
52
	 * @param IManager $activityManager
53
	 * @param Factory $parameterFactory
54
	 * @param IFactory $l10Nfactory
55
	 * @param IL10N $l
56
	 * @param Manager $groupManager
57
	 */
58 35
	public function __construct(IManager $activityManager, Factory $parameterFactory, IFactory $l10Nfactory, IL10N $l, Manager $groupManager) {
59 35
		$this->activityManager = $activityManager;
60 35
		$this->parameterFactory = $parameterFactory;
61 35
		$this->l10Nfactory = $l10Nfactory;
62 35
		$this->l = $l;
63 35
		$this->groupManager = $groupManager;
64 35
	}
65
66
	/**
67
	 * @param string $user
68
	 */
69 6
	public function setUser($user) {
70 6
		$this->parameterFactory->setUser($user);
71 6
	}
72
73
	/**
74
	 * @param IL10N $l
75
	 */
76 1
	public function setL10n(IL10N $l) {
77 1
		$this->parameterFactory->setL10n($l);
78 1
		$this->l = $l;
79 1
	}
80
81
	/**
82
	 * @brief Translate an event string with the translations from the app where it was send from
83
	 * @param string $app The app where this event comes from
84
	 * @param string $text The text including placeholders
85
	 * @param IParameter[] $params The parameter for the placeholder
86
	 * @return string translated
87
	 */
88 10
	public function translation($app, $text, array $params) {
89 10
		if (!$text) {
90 5
			return '';
91
		}
92
93 9
		$preparedParams = [];
94 9
		foreach ($params as $parameter) {
95 6
			$preparedParams[] = $parameter->format();
96 9
		}
97
98
		// Allow apps to correctly translate their activities
99 9
		$translation = $this->activityManager->translate(
100 9
			$app, $text, $preparedParams, false, false, $this->l->getLanguageCode());
101
102 9
		if ($translation !== false) {
103 6
			return $translation;
104
		}
105
106 3
		$l = $this->l10Nfactory->get($app, $this->l->getLanguageCode());
107 4
		return $l->t($text, $preparedParams);
108
	}
109
110
	/**
111
	 * List with special parameters for the message
112
	 *
113
	 * @param string $app
114
	 * @param string $text
115
	 * @return array
116
	 */
117 7
	protected function getSpecialParameterList($app, $text) {
118 7
		$specialParameters = $this->activityManager->getSpecialParameterList($app, $text);
119
120 7
		if ($specialParameters !== false) {
121 6
			return $specialParameters;
122
		}
123
124 5
		return array();
125
	}
126
127
	/**
128
	 * Format strings for display
129
	 *
130
	 * @param array $activity
131
	 * @param string $message 'subject' or 'message'
132
	 * @return array Modified $activity
133
	 */
134 6
	public function formatStrings($activity, $message) {
135 6
		$activity[$message . 'params'] = $activity[$message . 'params_array'];
136 6
		unset($activity[$message . 'params_array']);
137 6
		$activity[$message . '_prepared'] = $this->translation($activity['app'], $activity[$message], $activity[$message . 'params']);
138
139 6
		return $activity;
140
	}
141
142
	/**
143
	 * Get the parameter array from the parameter string of the database table
144
	 *
145
	 * @param IEvent $event
146
	 * @param string $parsing What are we parsing `message` or `subject`
147
	 * @param string $parameterString can be a JSON string, serialize() or a simple string.
148
	 * @return array List of Parameters
149
	 */
150 8
	public function getParameters(IEvent $event, $parsing, $parameterString) {
151 8
		$parameters = $this->parseParameters($parameterString);
152 8
		$parameterTypes = $this->getSpecialParameterList(
153 8
			$event->getApp(),
154 8
			($parsing === 'subject') ? $event->getSubject() : $event->getMessage()
155 8
		);
156
157 8
		foreach ($parameters as $i => $parameter) {
158 7
			$parameters[$i] = $this->parameterFactory->get(
159 7
				$parameter,
160 7
				$event,
161 7
				isset($parameterTypes[$i]) ? $parameterTypes[$i] : 'base'
162 7
			);
163 8
		}
164
165 8
		return $parameters;
166
	}
167
168
	/**
169
	 * @return Collection
170
	 */
171 1
	public function createCollection() {
172 1
		return $this->parameterFactory->createCollection();
173
	}
174
175
	/**
176
	 * Get the parameter array from the parameter string of the database table
177
	 *
178
	 * @param string $parameterString can be a JSON string, serialize() or a simple string.
179
	 * @return array List of Parameters
180
	 */
181 11
	public function parseParameters($parameterString) {
182 11
		if (!is_string($parameterString)) {
183 1
			return [];
184
		}
185 10
		$parameters = $parameterString;
186
187 10
		if ($parameterString[0] === '[' && substr($parameterString, -1) === ']' || $parameterString[0] === '"' && substr($parameterString, -1) === '"') {
188
			// ownCloud 8.1+
189 7
			$parameters = json_decode($parameterString, true);
190
191
			/*
192
			 * Remove gid from the parameters array
193
			 * While displaying in the UI we want only
194
			 * displayName. Convert the array to string.
195
			 */
196 7
			if((count($parameters) > 1) && is_array($parameters[1])) {
197 1
				if(isset($parameters[1]['gid'])) {
198 1
					if(isset($parameters[1]['displayName'])) {
199 1
						if ($parameters[1]['displayName'] !==
200 1
							$this->groupManager->get($parameters[1]['gid'])->getDisplayName()) {
201
							$parameters[1]['displayName'] = $this->groupManager->get($parameters[1]['gid'])->getDisplayName();
202
						}
203 1
						$parameters[1] = $parameters[1]['displayName'];
204 1
					}
205 1
				}
206 1
			}
207
208 7
			if ($parameters === null) {
209
				// Error on json decode
210 1
				$parameters = $parameterString;
211 1
			}
212
213 10
		} else if (isset($parameterString[7]) && $parameterString[1] === ':' && ($parameterString[0] === 's' && substr($parameterString, -1) === ';' || $parameterString[0] === 'a' && substr($parameterString, -1) === '}')) {
214
			// ownCloud 7+
215
			// Min length 8: `s:1:"a";`
216
			// Accepts: `s:1:"a";` for single string `a:1:{i:0;s:1:"a";}` for array
217 2
			$parameters = unserialize($parameterString);
218 2
		}
219
220 10
		if (is_array($parameters)) {
221 7
			return $parameters;
222
		}
223
224
		// ownCloud <7
225 3
		return [$parameters];
226
	}
227
}
228