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

FileSize::executeCheck()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 2
dl 0
loc 17
rs 9.2222
c 0
b 0
f 0
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 OCA\WorkflowEngine\Entity\File;
26
use OCP\IL10N;
27
use OCP\IRequest;
28
use OCP\Util;
29
use OCP\WorkflowEngine\ICheck;
30
use OCP\WorkflowEngine\IEntity;
31
32
class FileSize implements ICheck {
33
34
	/** @var int */
35
	protected $size;
36
37
	/** @var IL10N */
38
	protected $l;
39
40
	/** @var IRequest */
41
	protected $request;
42
43
	/**
44
	 * @param IL10N $l
45
	 * @param IRequest $request
46
	 */
47
	public function __construct(IL10N $l, IRequest $request) {
48
		$this->l = $l;
49
		$this->request = $request;
50
	}
51
52
	/**
53
	 * @param string $operator
54
	 * @param string $value
55
	 * @return bool
56
	 */
57
	public function executeCheck($operator, $value) {
58
		$size = $this->getFileSizeFromHeader();
59
60
		$value = Util::computerFileSize($value);
61
		if ($size !== false) {
0 ignored issues
show
introduced by
The condition $size !== false is always true.
Loading history...
62
			switch ($operator) {
63
				case 'less':
64
					return $size < $value;
65
				case '!less':
66
					return $size >= $value;
67
				case 'greater':
68
					return $size > $value;
69
				case '!greater':
70
					return $size <= $value;
71
			}
72
		}
73
		return false;
74
	}
75
76
	/**
77
	 * @param string $operator
78
	 * @param string $value
79
	 * @throws \UnexpectedValueException
80
	 */
81
	public function validateCheck($operator, $value) {
82
		if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) {
83
			throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
84
		}
85
86
		if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) {
87
			throw new \UnexpectedValueException($this->l->t('The given file size is invalid'), 2);
88
		}
89
	}
90
91
	/**
92
	 * @return string
93
	 */
94
	protected function getFileSizeFromHeader() {
95
		if ($this->size !== null) {
96
			return $this->size;
97
		}
98
99
		$size = $this->request->getHeader('OC-Total-Length');
100
		if ($size === '') {
101
			if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
102
				$size = $this->request->getHeader('Content-Length');
103
			}
104
		}
105
106
		if ($size === '') {
107
			$size = false;
108
		}
109
110
		$this->size = $size;
0 ignored issues
show
Documentation Bug introduced by
It seems like $size of type false or string is incompatible with the declared type integer of property $size.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
111
		return $this->size;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->size could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
112
	}
113
114
	public function supportedEntities(): array {
115
		return [ File::class ];
116
	}
117
118
	public function isAvailableForScope(int $scope): bool {
119
		return true;
120
	}
121
}
122