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

FileSize::executeCheck()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 14
c 1
b 0
f 1
nc 6
nop 2
dl 0
loc 18
rs 8.8571
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\Files\Storage\IStorage;
26
use OCP\IL10N;
27
use OCP\IRequest;
28
use OCP\Util;
29
use OCP\WorkflowEngine\ICheck;
30
31
class FileSize implements ICheck {
32
33
	/** @var int */
34
	protected $size;
35
36
	/** @var IL10N */
37
	protected $l;
38
39
	/** @var IRequest */
40
	protected $request;
41
42
	/**
43
	 * @param IL10N $l
44
	 * @param IRequest $request
45
	 */
46
	public function __construct(IL10N $l, IRequest $request) {
47
		$this->l = $l;
48
		$this->request = $request;
49
	}
50
51
	/**
52
	 * @param IStorage $storage
53
	 * @param string $path
54
	 */
55
	public function setFileInfo(IStorage $storage, $path) {
56
	}
57
58
	/**
59
	 * @param string $operator
60
	 * @param string $value
61
	 * @return bool
62
	 */
63
	public function executeCheck($operator, $value) {
64
		$size = $this->getFileSizeFromHeader();
65
66
		$value = Util::computerFileSize($value);
67
		if ($size !== false) {
68
			switch ($operator) {
69
				case 'less':
70
					return $size < $value;
71
				case '!less':
72
					return $size >= $value;
73
				case 'greater':
74
					return $size > $value;
75
				case '!greater':
76
					return $size <= $value;
77
			}
78
		}
79
		return false;
80
	}
81
82
	/**
83
	 * @param string $operator
84
	 * @param string $value
85
	 * @throws \UnexpectedValueException
86
	 */
87
	public function validateCheck($operator, $value) {
88 View Code Duplication
		if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
			throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
90
		}
91
92
		if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) {
93
			throw new \UnexpectedValueException($this->l->t('The given file size is invalid'), 2);
94
		}
95
	}
96
97
	/**
98
	 * @return string
99
	 */
100
	protected function getFileSizeFromHeader() {
101
		if ($this->size !== null) {
102
			return $this->size;
103
		}
104
105
		$size = $this->request->getHeader('OC-Total-Length');
106
		if ($size === null) {
107
			if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
108
				$size = $this->request->getHeader('Content-Length');
109
			}
110
		}
111
112
		if ($size === null) {
113
			$size = false;
114
		}
115
116
		$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...
117
		return $this->size;
118
	}
119
}
120