Completed
Pull Request — master (#7)
by Joas
124:30 queued 122:31
created

Extension   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 177
ccs 45
cts 45
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNotificationTypes() 0 3 1
A getDefaultTypes() 0 3 1
A getTypeIcon() 0 3 2
A getGroupParameter() 0 3 1
A getNavigation() 0 3 1
A isFilterValid() 0 3 1
A filterNotificationTypes() 0 7 2
A getQueryForFilter() 0 3 1
B translate() 0 31 6
A getSpecialParameterList() 0 8 3
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
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\AnnouncementCenter\Activity;
23
24
use OCA\AnnouncementCenter\Manager;
25
use OCP\Activity\IExtension;
26
use OCP\Activity\IManager;
27
use OCP\L10N\IFactory;
28
29
class Extension implements IExtension {
30
	/** @var Manager */
31
	protected $manager;
32
	/** @var IManager */
33
	protected $activityManager;
34
	/** @var IFactory */
35
	protected $languageFactory;
36
37
	/**
38
	 * @param Manager $manager
39
	 * @param IManager $activityManager
40
	 * @param IFactory $languageFactory
41
	 */
42 29
	public function __construct(Manager $manager, IManager $activityManager, IFactory $languageFactory) {
43 29
		$this->manager = $manager;
44 29
		$this->activityManager = $activityManager;
45 29
		$this->languageFactory = $languageFactory;
46 29
	}
47
48
	/**
49
	 * The extension can return an array of additional notification types.
50
	 * If no additional types are to be added false is to be returned
51
	 *
52
	 * @param string $languageCode
53
	 * @return array|false
54
	 */
55 1
	public function getNotificationTypes($languageCode) {
56 1
		return false;
57
	}
58
59
	/**
60
	 * For a given method additional types to be displayed in the settings can be returned.
61
	 * In case no additional types are to be added false is to be returned.
62
	 *
63
	 * @param string $method
64
	 * @return array|false
65
	 */
66 2
	public function getDefaultTypes($method) {
67 2
		return ['announcementcenter'];
68
	}
69
70
	/**
71
	 * A string naming the css class for the icon to be used can be returned.
72
	 * If no icon is known for the given type false is to be returned.
73
	 *
74
	 * @param string $type
75
	 * @return string|false
76
	 */
77 2
	public function getTypeIcon($type) {
78 2
		return $type === 'announcementcenter' ? 'icon-info' : false;
79
	}
80
81
	/**
82
	 * The extension can translate a given message to the requested languages.
83
	 * If no translation is available false is to be returned.
84
	 *
85
	 * @param string $app
86
	 * @param string $text
87
	 * @param array $params
88
	 * @param boolean $stripPath
89
	 * @param boolean $highlightParams
90
	 * @param string $languageCode
91
	 * @return string|false
92
	 */
93 6
	public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
94 6
		if ($app === 'announcementcenter') {
95 5
			$l = $this->languageFactory->get('announcementcenter', $languageCode);
96
97 5
			list(, $id) = explode('#', $text);
98
99
			try {
100 5
				$announcement = $this->manager->getAnnouncement($id, true);
101 5
			} catch (\InvalidArgumentException $e) {
102 1
				return (string) $l->t('Announcement does not exist anymore', $params);
103
			}
104
105 4
			if (strpos($text, 'announcementmessage#') === 0) {
106 1
				return $announcement['message'];
107
			}
108
109 3
			$params[] = '<parameter>' . $announcement['subject'] . '</parameter>';
110
111
			try {
112 3
				if ($announcement['author'] === $this->activityManager->getCurrentUserId()) {
113 1
					array_shift($params);
114 1
					return (string) $l->t('You announced %s', $params);
115
				}
116 2
			} catch (\UnexpectedValueException $e) {
117
				// FIXME this is awkward, but we have no access to the current user in emails
118
			}
119 2
			return (string) $l->t('%s announced %s', $params);
120
		}
121
122 1
		return false;
123
	}
124
125
	/**
126
	 * The extension can define the type of parameters for translation
127
	 *
128
	 * Currently known types are:
129
	 * * file		=> will strip away the path of the file and add a tooltip with it
130
	 * * username	=> will add the avatar of the user
131
	 *
132
	 * @param string $app
133
	 * @param string $text
134
	 * @return array|false
135
	 */
136 3
	public function getSpecialParameterList($app, $text) {
137 3
		if ($app === 'announcementcenter'&& strpos($text, 'announcementsubject#') === 0) {
138
			return [
139 1
				0 => 'username',
140 1
			];
141
		}
142 2
		return false;
143
	}
144
145
	/**
146
	 * The extension can define the parameter grouping by returning the index as integer.
147
	 * In case no grouping is required false is to be returned.
148
	 *
149
	 * @param array $activity
150
	 * @return integer|false
151
	 */
152 1
	public function getGroupParameter($activity) {
153 1
		return false;
154
	}
155
156
	/**
157
	 * The extension can define additional navigation entries. The array returned has to contain two keys 'top'
158
	 * and 'apps' which hold arrays with the relevant entries.
159
	 * If no further entries are to be added false is no be returned.
160
	 *
161
	 * @return array|false
162
	 */
163 1
	public function getNavigation() {
164 1
		return false;
165
	}
166
167
	/**
168
	 * The extension can check if a customer filter (given by a query string like filter=abc) is valid or not.
169
	 *
170
	 * @param string $filterValue
171
	 * @return boolean
172
	 */
173 4
	public function isFilterValid($filterValue) {
174 4
		return false;
175
	}
176
177
	/**
178
	 * The extension can filter the types based on the filter if required.
179
	 * In case no filter is to be applied false is to be returned unchanged.
180
	 *
181
	 * @param array $types
182
	 * @param string $filter
183
	 * @return array|false
184
	 */
185 4
	public function filterNotificationTypes($types, $filter) {
186 4
		if (in_array($filter, ['all', 'by', 'self'])) {
187 3
			$types[] = 'announcementcenter';
188 3
			return $types;
189
		}
190 1
		return false;
191
	}
192
193
	/**
194
	 * For a given filter the extension can specify the sql query conditions including parameters for that query.
195
	 * In case the extension does not know the filter false is to be returned.
196
	 * The query condition and the parameters are to be returned as array with two elements.
197
	 * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%'));
198
	 *
199
	 * @param string $filter
200
	 * @return array|false
201
	 */
202 4
	public function getQueryForFilter($filter) {
203 4
		return false;
204
	}
205
}
206