Completed
Pull Request — master (#120)
by Morris
08:46
created

GroupHelper   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.22%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 174
ccs 74
cts 90
cp 0.8222
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setUser() 0 3 1
A setL10n() 0 4 1
D addActivity() 0 37 9
A getActivities() 0 9 2
A arrayToEvent() 0 14 1
B eventToArray() 0 26 1
A getObjectsFromChildren() 0 10 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program 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 License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Activity;
24
25
use OCA\Activity\Extension\LegacyParser;
26
use OCA\Activity\Parameter\IParameter;
27
use OCP\Activity\IEvent;
28
use OCP\Activity\IManager;
29
use OCP\IL10N;
30
31
class GroupHelper {
32
	/** @var IEvent[] */
33
	protected $event = [];
34
	/** @var int */
35
	protected $lastEvent = 0;
36
37
	/** @var bool */
38
	protected $allowGrouping;
39
40
	/** @var IL10N */
41
	protected $l;
42
43
	/** @var \OCP\Activity\IManager */
44
	protected $activityManager;
45
46
	/** @var \OCA\Activity\DataHelper */
47
	protected $dataHelper;
48
49
	/** @var LegacyParser */
50
	protected $legacyParser;
51
52
	/**
53
	 * @param IL10N $l
54
	 * @param \OCP\Activity\IManager $activityManager
55
	 * @param \OCA\Activity\DataHelper $dataHelper
56
	 * @param LegacyParser $legacyParser
57
	 */
58 20
	public function __construct(IL10N $l, IManager $activityManager, DataHelper $dataHelper, LegacyParser $legacyParser) {
59 20
		$this->allowGrouping = true;
60
61 20
		$this->l = $l;
62 20
		$this->activityManager = $activityManager;
63 20
		$this->dataHelper = $dataHelper;
64 20
		$this->legacyParser = $legacyParser;
65 20
	}
66
67
	/**
68
	 * @param string $user
69
	 */
70 6
	public function setUser($user) {
71 6
		$this->dataHelper->setUser($user);
72 6
	}
73
74
	/**
75
	 * @param IL10N $l
76
	 */
77 1
	public function setL10n(IL10N $l) {
78 1
		$this->l = $l;
79 1
		$this->dataHelper->setL10n($l);
80 1
	}
81
82
	/**
83
	 * Add an activity to the internal array
84
	 *
85
	 * @param array $activity
86
	 */
87 4
	public function addActivity($activity) {
88 4
		$id = (int) $activity['activity_id'];
89 4
		$event = $this->arrayToEvent($activity);
90 4
		$language = $this->l->getLanguageCode();
91
92 4
		foreach ($this->activityManager->getProviders() as $provider) {
93
			try {
94
				$this->activityManager->setFormattingObject($event->getObjectType(), $event->getObjectId());
95
				if ($this->allowGrouping && $this->lastEvent !== 0 && isset($this->event[$this->lastEvent])) {
96
					$event = $provider->parse($language, $event, $this->event[$this->lastEvent]);
97
				} else {
98
					$event = $provider->parse($language, $event);
99
				}
100
				$this->activityManager->setFormattingObject('', 0);
101
102
				$child = $event->getChildEvent();
103
				if ($child instanceof IEvent) {
0 ignored issues
show
Bug introduced by
The class OCP\Activity\IEvent 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...
104
					unset($this->event[$this->lastEvent]);
105
				}
106
			} catch (\InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
107
			}
108 4
		}
109
110 4
		if (!$event->getParsedSubject()) {
111
			try {
112 4
				$this->activityManager->setFormattingObject($event->getObjectType(), $event->getObjectId());
113 4
				$event = $this->legacyParser->parse($language, $event);
114 4
				$this->activityManager->setFormattingObject('', 0);
115 4
			} catch (\InvalidArgumentException $e) {
116
				\OC::$server->getLogger()->debug('Failed to parse activity');
117
				return;
118
			}
119 4
		}
120
121 4
		$this->event[$id] = $event;
122 4
		$this->lastEvent = $id;
123 4
	}
124
125
	/**
126
	 * Get the prepared activities
127
	 *
128
	 * @return array translated activities ready for use
129
	 */
130 5
	public function getActivities() {
131 5
		$return = [];
132 5
		foreach ($this->event as $id => $event) {
133 4
			$return[] = $this->eventToArray($event, $id);
134 5
		}
135 5
		$this->event = [];
136
137 5
		return $return;
138
	}
139
140
	/**
141
	 * @param array $row
142
	 * @return IEvent
143
	 */
144 6
	protected function arrayToEvent(array $row) {
145 6
		$event = $this->activityManager->generateEvent();
146 6
		$event->setApp((string) $row['app'])
147 6
			->setType((string) $row['type'])
148 6
			->setAffectedUser((string) $row['affecteduser'])
149 6
			->setAuthor((string) $row['user'])
150 6
			->setTimestamp((int) $row['timestamp'])
151 6
			->setSubject((string) $row['subject'], json_decode($row['subjectparams'], true))
152 6
			->setMessage((string) $row['message'], json_decode($row['messageparams'], true))
153 6
			->setObject((string) $row['object_type'], (int) $row['object_id'], (string) $row['file'])
154 6
			->setLink((string) $row['link']);
155
156 6
		return $event;
157
	}
158
159
	/**
160
	 * @param IEvent $event
161
	 * @return array
162
	 */
163 4
	protected function eventToArray(IEvent $event, $id) {
164
		return [
165 4
			'activity_id' => $id,
166 4
			'app' => $event->getApp(),
167 4
			'type' => $event->getType(),
168 4
			'affecteduser' => $event->getAffectedUser(),
169 4
			'user' => $event->getAuthor(),
170 4
			'timestamp' => $event->getTimestamp(),
171 4
			'subject' => $event->getParsedSubject(),
172
			'subject_rich' => [
173 4
				(string) $event->getRichSubject(),
174 4
				(array) $event->getRichSubjectParameters(),
175 4
			],
176 4
			'message' => $event->getParsedMessage(),
177
			'message_rich' => [
178 4
				(string) $event->getRichMessage(),
179 4
				(array) $event->getRichMessageParameters(),
180 4
			],
181 4
			'object_type' => $event->getObjectType(),
182 4
			'object_id' => $event->getObjectId(),
183 4
			'object_name' => $event->getObjectName(),
184 4
			'objects' => $this->getObjectsFromChildren($event),
185 4
			'link' => $event->getLink(),
186 4
			'icon' => $event->getIcon(),
187 4
		];
188
	}
189
190
	/**
191
	 * @param IEvent $event
192
	 * @return array
193
	 */
194 4
	protected function getObjectsFromChildren(IEvent $event) {
195 4
		$child = $event->getChildEvent();
196 4
		if ($child instanceof IEvent) {
0 ignored issues
show
Bug introduced by
The class OCP\Activity\IEvent 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...
197
			$objects = $this->getObjectsFromChildren($child);
198
			$objects[$event->getObjectId()] = $event->getObjectName();
199
			return $objects;
200
		} else {
201 4
			return [$event->getObjectId() => $event->getObjectName()];
202
		}
203
	}
204
}
205