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

AbstractStringCheck   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 93
rs 10
wmc 14
lcom 2
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setFileInfo() 0 3 1
getActualValue() 0 1 ?
A executeCheck() 0 4 1
A executeStringCheck() 0 14 4
A match() 0 12 3
A validateCheck() 0 10 4
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\WorkflowEngine\ICheck;
28
29
abstract class AbstractStringCheck implements ICheck {
30
31
	/** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */
32
	protected $matches;
33
34
	/** @var IL10N */
35
	protected $l;
36
37
	/**
38
	 * @param IL10N $l
39
	 */
40
	public function __construct(IL10N $l) {
41
		$this->l = $l;
42
	}
43
44
	/**
45
	 * @param IStorage $storage
46
	 * @param string $path
47
	 */
48
	public function setFileInfo(IStorage $storage, $path) {
49
		// Nothing changes here with a different path
50
	}
51
52
	/**
53
	 * @return string
54
	 */
55
	abstract protected function getActualValue();
56
57
	/**
58
	 * @param string $operator
59
	 * @param string $value
60
	 * @return bool
61
	 */
62
	public function executeCheck($operator, $value)  {
63
		$actualValue = $this->getActualValue();
64
		return $this->executeStringCheck($operator, $value, $actualValue);
65
	}
66
67
	/**
68
	 * @param string $operator
69
	 * @param string $checkValue
70
	 * @param string $actualValue
71
	 * @return bool
72
	 */
73
	protected function executeStringCheck($operator, $checkValue, $actualValue) {
74
		if ($operator === 'is') {
75
			return $checkValue === $actualValue;
76
		} else if ($operator === '!is') {
77
			return $checkValue !== $actualValue;
78
		} else {
79
			$match = $this->match($checkValue, $actualValue);
80
			if ($operator === 'matches') {
81
				return $match === 1;
82
			} else {
83
				return $match === 0;
84
			}
85
		}
86
	}
87
88
	/**
89
	 * @param string $operator
90
	 * @param string $value
91
	 * @throws \UnexpectedValueException
92
	 */
93
	public function validateCheck($operator, $value) {
94
		if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) {
95
			throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
96
		}
97
98
		if (in_array($operator, ['matches', '!matches']) &&
99
			  @preg_match($value, null) === false) {
100
			throw new \UnexpectedValueException($this->l->t('The given regular expression is invalid'), 2);
101
		}
102
	}
103
104
	/**
105
	 * @param string $pattern
106
	 * @param string $subject
107
	 * @return int|bool
108
	 */
109
	protected function match($pattern, $subject) {
110
		$patternHash = md5($pattern);
111
		$subjectHash = md5($subject);
112
		if (isset($this->matches[$patternHash][$subjectHash])) {
113
			return $this->matches[$patternHash][$subjectHash];
114
		}
115
		if (!isset($this->matches[$patternHash])) {
116
			$this->matches[$patternHash] = [];
117
		}
118
		$this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject);
119
		return $this->matches[$patternHash][$subjectHash];
120
	}
121
}
122