Passed
Push — master ( 2187f8...15d39c )
by Joas
11:45 queued 12s
created

File::prepareRuleMatcher()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 19
c 1
b 0
f 0
nc 10
nop 3
dl 0
loc 22
rs 6.9666

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
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019 Arthur Schiwon <[email protected]>
5
 *
6
 * @author Arthur Schiwon <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\WorkflowEngine\Entity;
26
27
use OCP\Files\IRootFolder;
28
use OCP\IL10N;
29
use OCP\IURLGenerator;
30
use OCP\SystemTag\MapperEvent;
31
use OCP\WorkflowEngine\GenericEntityEvent;
32
use OCP\WorkflowEngine\IEntity;
33
use OCP\WorkflowEngine\IRuleMatcher;
34
use Symfony\Component\EventDispatcher\GenericEvent;
35
36
class File implements IEntity {
37
38
	/** @var IL10N */
39
	protected $l10n;
40
	/** @var IURLGenerator */
41
	protected $urlGenerator;
42
	/** @var IRootFolder */
43
	private $root;
44
45
	public function __construct(IL10N $l10n, IURLGenerator $urlGenerator, IRootFolder $root) {
46
		$this->l10n = $l10n;
47
		$this->urlGenerator = $urlGenerator;
48
		$this->root = $root;
49
	}
50
51
	public function getName(): string {
52
		return $this->l10n->t('File');
53
	}
54
55
	public function getIcon(): string {
56
		return $this->urlGenerator->imagePath('core', 'categories/files.svg');
57
	}
58
59
	public function getEvents(): array {
60
		$namespace = '\OCP\Files::';
61
		return [
62
			new GenericEntityEvent($this->l10n->t('File created'), $namespace . 'postCreate' ),
63
			new GenericEntityEvent($this->l10n->t('File updated'), $namespace . 'postWrite' ),
64
			new GenericEntityEvent($this->l10n->t('File renamed'), $namespace . 'postRename' ),
65
			new GenericEntityEvent($this->l10n->t('File deleted'), $namespace . 'postDelete' ),
66
			new GenericEntityEvent($this->l10n->t('File accessed'), $namespace . 'postTouch' ),
67
			new GenericEntityEvent($this->l10n->t('File copied'), $namespace . 'postCopy' ),
68
			new GenericEntityEvent($this->l10n->t('Tag assigned'), MapperEvent::EVENT_ASSIGN ),
69
		];
70
	}
71
72
	/**
73
	 * @since 18.0.0
74
	 */
75
	public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, GenericEvent $event): void {
76
		switch ($eventName) {
77
			case 'postCreate':
78
			case 'postWrite':
79
			case 'postDelete':
80
			case 'postTouch':
81
				$ruleMatcher->setEntitySubject($this, $event->getSubject());
82
				break;
83
			case 'postRename':
84
			case 'postCopy':
85
				$ruleMatcher->setEntitySubject($this, $event->getSubject()[1]);
86
				break;
87
			case MapperEvent::EVENT_ASSIGN:
88
				if(!$event instanceof MapperEvent || $event->getObjectType() !== 'files') {
0 ignored issues
show
introduced by
$event is never a sub-type of OCP\SystemTag\MapperEvent.
Loading history...
89
					break;
90
				}
91
				$nodes = $this->root->getById((int)$event->getObjectId());
92
				if(is_array($nodes) && !empty($nodes)) {
93
					$node = array_shift($nodes);
94
					$ruleMatcher->setEntitySubject($this, $node);
95
				}
96
				break;
97
		}
98
	}
99
}
100