Completed
Push — master ( 60d45e...348d97 )
by Joas
14:45
created

LegacyHooks   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A preUnshare() 0 7 1
A postUnshare() 0 17 1
B formatHookParams() 0 23 5
1
<?php
2
/**
3
 * @copyright 2017, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\Share20;
24
25
use OCP\Share\IShare;
26
use Symfony\Component\EventDispatcher\EventDispatcher;
27
use Symfony\Component\EventDispatcher\GenericEvent;
28
29
class LegacyHooks {
30
	/** @var EventDispatcher */
31
	private $eventDispatcher;
32
33
	/**
34
	 * LegacyHooks constructor.
35
	 *
36
	 * @param EventDispatcher $eventDispatcher
37
	 */
38
	public function __construct(EventDispatcher $eventDispatcher) {
39
		$this->eventDispatcher = $eventDispatcher;
40
41
		$this->eventDispatcher->addListener('OCP\Share::preUnshare', [$this, 'preUnshare']);
42
		$this->eventDispatcher->addListener('OCP\Share::postUnshare', [$this, 'postUnshare']);
43
	}
44
45
	/**
46
	 * @param GenericEvent $e
47
	 */
48
	public function preUnshare(GenericEvent $e) {
49
		/** @var IShare $share */
50
		$share = $e->getSubject();
51
52
		$formatted = $this->formatHookParams($share);
53
		\OC_Hook::emit('OCP\Share', 'pre_unshare', $formatted);
54
	}
55
56
	/**
57
	 * @param GenericEvent $e
58
	 */
59
	public function postUnshare(GenericEvent $e) {
60
		/** @var IShare $share */
61
		$share = $e->getSubject();
62
63
		$formatted = $this->formatHookParams($share);
64
65
		/** @var IShare[] $deletedShares */
66
		$deletedShares = $e->getArgument('deletedShares');
67
68
		$formattedDeletedShares = array_map(function($share) {
69
			return $this->formatHookParams($share);
70
		}, $deletedShares);
71
72
		$formatted['deletedShares'] = $formattedDeletedShares;
73
74
		\OC_Hook::emit('OCP\Share', 'pre_unshare', $formatted);
75
	}
76
77
	private function formatHookParams(IShare $share) {
78
		// Prepare hook
79
		$shareType = $share->getShareType();
80
		$sharedWith = '';
81
		if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
82
			$shareType === \OCP\Share::SHARE_TYPE_GROUP ||
83
			$shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
84
			$sharedWith = $share->getSharedWith();
85
		}
86
87
		$hookParams = [
88
			'id' => $share->getId(),
89
			'itemType' => $share->getNodeType(),
90
			'itemSource' => $share->getNodeId(),
91
			'shareType' => $shareType,
92
			'shareWith' => $sharedWith,
93
			'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
94
			'uidOwner' => $share->getSharedBy(),
95
			'fileSource' => $share->getNodeId(),
96
			'fileTarget' => $share->getTarget()
97
		];
98
		return $hookParams;
99
	}
100
}
101