Completed
Push — master ( 8bf574...c0bbae )
by Lukas
28:01 queued 15:22
created

Watcher   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 37.7 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 23
loc 61
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A postWrite() 13 13 3
B preDelete() 0 19 5
A postDelete() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, 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\Preview;
24
25
use OCP\Files\File;
26
use OCP\Files\Node;
27
use OCP\Files\Folder;
28
use OCP\Files\IAppData;
29
use OCP\Files\NotFoundException;
30
31
/**
32
 * Class Watcher
33
 *
34
 * @package OC\Preview
35
 *
36
 * Class that will watch filesystem activity and remove previews as needed.
37
 */
38
class Watcher {
39
	/** @var IAppData */
40
	private $appData;
41
42
	/** @var int[] */
43
	private $toDelete = [];
44
45
	/**
46
	 * Watcher constructor.
47
	 *
48
	 * @param IAppData $appData
49
	 */
50
	public function __construct(IAppData $appData) {
51
		$this->appData = $appData;
52
	}
53
54 View Code Duplication
	public function postWrite(Node $node) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
		// We only handle files
56
		if ($node instanceof Folder) {
57
			return;
58
		}
59
60
		try {
61
			$folder = $this->appData->getFolder($node->getId());
62
			$folder->delete();
63
		} catch (NotFoundException $e) {
64
			//Nothing to do
65
		}
66
	}
67
68
	public function preDelete(Node $node) {
69
		// To avoid cycles
70
		if ($this->toDelete !== []) {
71
			return;
72
		}
73
74
		if ($node instanceof File) {
75
			$this->toDelete[] = $node->getId();
76
			return;
77
		}
78
79
		/** @var Folder $node */
80
		$nodes = $node->search('');
81
		foreach ($nodes as $node) {
82
			if ($node instanceof File) {
83
				$this->toDelete[] = $node->getId();
84
			}
85
		}
86
	}
87
88 View Code Duplication
	public function postDelete(Node $node) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
		foreach ($this->toDelete as $fid) {
90
			try {
91
				$folder = $this->appData->getFolder($fid);
92
				$folder->delete();
93
			} catch (NotFoundException $e) {
94
				// continue
95
			}
96
		}
97
	}
98
}
99