RemoteActivity::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Activity\BackgroundJob;
23
24
use GuzzleHttp\Exception\ClientException;
25
use OC\BackgroundJob\QueuedJob;
26
use OCA\Activity\Extension\Files;
27
use OCP\Federation\ICloudId;
28
use OCP\Federation\ICloudIdManager;
29
use OCP\Http\Client\IClientService;
30
31
class RemoteActivity extends QueuedJob {
32
33
	/** @var IClientService */
34
	protected $clientService;
35
36
	/** @var ICloudIdManager */
37
	protected $cloudIdManager;
38
39
	public function __construct(IClientService $clientService, ICloudIdManager $cloudIdManager) {
40
		$this->clientService = $clientService;
41
		$this->cloudIdManager = $cloudIdManager;
42
	}
43
44
	protected function run($arguments) {
45
		call_user_func_array([$this, 'sendActivity'], $arguments);
46
	}
47
48
	protected function sendActivity($target, $token, $path, $internalType, $time, $actor, $secondPath = '') {
49
		$client = $this->clientService->newClient();
50
51
		$cloudId = $this->cloudIdManager->resolveCloudId($target);
52
		$type = $this->translateType($internalType, $secondPath);
53
54
		$fields = [
55
			'@context' => 'https://www.w3.org/ns/activitystreams',
56
			'to' => [
57
				'type' => 'Person',
58
				'name' => $cloudId->getUser(),
59
			],
60
			'actor' => [
61
				'type' => 'Person',
62
				'name' => $actor,
63
			],
64
			'type' => $type,
65
			'updated' => date(\DateTime::W3C, $time),
66
		];
67
68
		if ($type === 'Move') {
69
			$fields['target'] = [
70
				'type' => 'Document',
71
				'name' => $path,
72
			];
73
			$fields['origin'] = [
74
				'type' => 'Document',
75
				'name' => $secondPath,
76
			];
77
		} else {
78
			$fields['object'] = [
79
				'type' => 'Document',
80
				'name' => $path,
81
			];
82
		}
83
84
		try {
85
			$client->post(
86
				$this->getServerURL($cloudId, $token), [
87
					'body' => $fields,
88
					'timeout' => 10,
89
					'connect_timeout' => 10,
90
				]
91
			);
92
		} catch (ClientException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class GuzzleHttp\Exception\ClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
93
		}
94
	}
95
96
	/**
97
	 * @param ICloudId $cloudId
98
	 * @param string $token
99
	 * @return string
100
	 */
101
	protected function getServerURL(ICloudId $cloudId, $token) {
102
		$remote = $cloudId->getRemote();
103
		if (strpos($remote, 'http') !== 0) {
104
			$remote = 'https://' . $remote;
105
		}
106
107
		return rtrim($remote, '/') . '/ocs/v2.php/apps/activity/api/v2/remote/' . $token;
108
	}
109
110
	/**
111
	 * @param string $internalType
112
	 * @param string $secondPath
113
	 * @return string
114
	 */
115
	protected function translateType($internalType, $secondPath) {
116
		switch ($internalType) {
117
			case Files::TYPE_SHARE_CREATED:
118
			case Files::TYPE_SHARE_RESTORED:
119
				return 'Create';
120
			case Files::TYPE_SHARE_CHANGED:
121
				if ($secondPath !== '') {
122
					return 'Move';
123
				}
124
				return 'Update';
125
			case Files::TYPE_SHARE_DELETED:
126
				return 'Delete';
127
		}
128
		return '';
129
	}
130
}
131