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

RequestTime::validateCheck()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 3
Ratio 13.64 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 5
eloc 14
c 3
b 0
f 1
nc 5
nop 2
dl 3
loc 22
rs 8.6737
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\AppFramework\Utility\ITimeFactory;
26
use OCP\Files\Storage\IStorage;
27
use OCP\IL10N;
28
use OCP\WorkflowEngine\ICheck;
29
30
class RequestTime implements ICheck {
31
32
	const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])';
33
	const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\/[a-zA-Z\-\_]+)+)';
34
35
	/** @var bool[] */
36
	protected $cachedResults;
37
38
	/** @var IL10N */
39
	protected $l;
40
41
	/** @var ITimeFactory */
42
	protected $timeFactory;
43
44
	/**
45
	 * @param ITimeFactory $timeFactory
46
	 */
47
	public function __construct(IL10N $l, ITimeFactory $timeFactory) {
48
		$this->l = $l;
49
		$this->timeFactory = $timeFactory;
50
	}
51
52
	/**
53
	 * @param IStorage $storage
54
	 * @param string $path
55
	 */
56
	public function setFileInfo(IStorage $storage, $path) {
57
		// A different path doesn't change time, so nothing to do here.
58
	}
59
60
	/**
61
	 * @param string $operator
62
	 * @param string $value
63
	 * @return bool
64
	 */
65
	public function executeCheck($operator, $value) {
66
		$valueHash = md5($value);
67
68
		if (isset($this->cachedResults[$valueHash])) {
69
			return $this->cachedResults[$valueHash];
70
		}
71
72
		$timestamp = $this->timeFactory->getTime();
73
74
		$values = json_decode($value, true);
75
		$timestamp1 = $this->getTimestamp($timestamp, $values[0]);
76
		$timestamp2 = $this->getTimestamp($timestamp, $values[1]);
77
78
		if ($timestamp1 < $timestamp2) {
79
			$in = $timestamp1 <= $timestamp && $timestamp <= $timestamp2;
80
		} else {
81
			$in = $timestamp1 <= $timestamp || $timestamp <= $timestamp2;
82
		}
83
84
		return ($operator === 'in') ? $in : !$in;
85
	}
86
87
	/**
88
	 * @param int $currentTimestamp
89
	 * @param string $value Format: "H:i e"
90
	 * @return int
91
	 */
92
	protected function getTimestamp($currentTimestamp, $value) {
93
		list($time1, $timezone1) = explode(' ', $value);
94
		list($hour1, $minute1) = explode(':', $time1);
95
		$date1 = new \DateTime('now', new \DateTimeZone($timezone1));
96
		$date1->setTimestamp($currentTimestamp);
97
		$date1->setTime($hour1, $minute1);
98
99
		return $date1->getTimestamp();
100
	}
101
102
	/**
103
	 * @param string $operator
104
	 * @param string $value
105
	 * @throws \UnexpectedValueException
106
	 */
107
	public function validateCheck($operator, $value) {
108 View Code Duplication
		if (!in_array($operator, ['in', '!in'])) {
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...
109
			throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
110
		}
111
112
		$regexValue = '\"' . self::REGEX_TIME . ' ' . self::REGEX_TIMEZONE . '\"';
113
		$result = preg_match('/^\[' . $regexValue . ',' . $regexValue . '\]$/', $value, $matches);
114
		if (!$result) {
115
			throw new \UnexpectedValueException($this->l->t('The given time span is invalid'), 2);
116
		}
117
118
		$values = json_decode($value, true);
119
		$time1 = \DateTime::createFromFormat('H:i e', $values[0]);
120
		if ($time1 === false) {
121
			throw new \UnexpectedValueException($this->l->t('The given start time is invalid'), 3);
122
		}
123
124
		$time2 = \DateTime::createFromFormat('H:i e', $values[1]);
125
		if ($time2 === false) {
126
			throw new \UnexpectedValueException($this->l->t('The given end time is invalid'), 4);
127
		}
128
	}
129
}
130