Completed
Push — stable10 ( d896d4...713e20 )
by Joas
26:40 queued 16:05
created

RequestURL   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 12.31 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 8
loc 65
rs 10
wmc 12
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A executeCheck() 0 14 4
A getActualValue() 0 12 2
B isWebDAVRequest() 8 8 5

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 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\WorkflowEngine\Check;
23
24
25
use OCP\IL10N;
26
use OCP\IRequest;
27
28
class RequestURL extends AbstractStringCheck {
29
30
	/** @var string */
31
	protected $url;
32
33
	/** @var IRequest */
34
	protected $request;
35
36
	/**
37
	 * @param IL10N $l
38
	 * @param IRequest $request
39
	 */
40
	public function __construct(IL10N $l, IRequest $request) {
41
		parent::__construct($l);
42
		$this->request = $request;
43
	}
44
45
	/**
46
	 * @param string $operator
47
	 * @param string $value
48
	 * @return bool
49
	 */
50
	public function executeCheck($operator, $value)  {
51
		$actualValue = $this->getActualValue();
52
		if (in_array($operator, ['is', '!is'])) {
53
			switch ($value) {
54
				case 'webdav':
55
					if ($operator === 'is') {
56
						return $this->isWebDAVRequest();
57
					} else {
58
						return !$this->isWebDAVRequest();
59
					}
60
			}
61
		}
62
		return $this->executeStringCheck($operator, $value, $actualValue);
63
	}
64
65
	/**
66
	 * @return string
67
	 */
68
	protected function getActualValue() {
69
		if ($this->url !== null) {
70
			return $this->url;
71
		}
72
73
		$this->url = $this->request->getServerProtocol() . '://';// E.g. http(s) + ://
74
		$this->url .= $this->request->getServerHost();// E.g. localhost
75
		$this->url .= $this->request->getScriptName();// E.g. /nextcloud/index.php
76
		$this->url .= $this->request->getPathInfo();// E.g. /apps/files_texteditor/ajax/loadfile
77
78
		return $this->url; // E.g. https://localhost/nextcloud/index.php/apps/files_texteditor/ajax/loadfile
79
	}
80
81
	/**
82
	 * @return bool
83
	 */
84 View Code Duplication
	protected function isWebDAVRequest() {
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...
85
		return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
86
			$this->request->getPathInfo() === '/webdav' ||
87
			strpos($this->request->getPathInfo(), '/webdav/') === 0 ||
88
			$this->request->getPathInfo() === '/dav/files' ||
89
			strpos($this->request->getPathInfo(), '/dav/files/') === 0
90
		);
91
	}
92
}
93