Completed
Push — stable10 ( 379260...cc80df )
by Lukas
35:03 queued 24:06
created

Manager::setFileInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Morris Jobke <[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;
23
24
25
use OCP\AppFramework\QueryException;
26
use OCP\DB\QueryBuilder\IQueryBuilder;
27
use OCP\Files\Storage\IStorage;
28
use OCP\IDBConnection;
29
use OCP\IL10N;
30
use OCP\IServerContainer;
31
use OCP\WorkflowEngine\ICheck;
32
use OCP\WorkflowEngine\IManager;
33
use OCP\WorkflowEngine\IOperation;
34
35
class Manager implements IManager {
36
37
	/** @var IStorage */
38
	protected $storage;
39
40
	/** @var string */
41
	protected $path;
42
43
	/** @var array[] */
44
	protected $operations = [];
45
46
	/** @var array[] */
47
	protected $checks = [];
48
49
	/** @var IDBConnection */
50
	protected $connection;
51
52
	/** @var IServerContainer|\OC\Server */
53
	protected $container;
54
55
	/** @var IL10N */
56
	protected $l;
57
58
	/**
59
	 * @param IDBConnection $connection
60
	 * @param IServerContainer $container
61
	 * @param IL10N $l
62
	 */
63
	public function __construct(IDBConnection $connection, IServerContainer $container, IL10N $l) {
64
		$this->connection = $connection;
65
		$this->container = $container;
66
		$this->l = $l;
67
	}
68
69
	/**
70
	 * @inheritdoc
71
	 */
72
	public function setFileInfo(IStorage $storage, $path) {
73
		$this->storage = $storage;
74
		$this->path = $path;
75
	}
76
77
	/**
78
	 * @inheritdoc
79
	 */
80
	public function getMatchingOperations($class, $returnFirstMatchingOperationOnly = true) {
81
		$operations = $this->getOperations($class);
82
83
		$matches = [];
84
		foreach ($operations as $operation) {
85
			$checkIds = json_decode($operation['checks'], true);
86
			$checks = $this->getChecks($checkIds);
87
88
			foreach ($checks as $check) {
89
				if (!$this->check($check)) {
90
					// Check did not match, continue with the next operation
91
					continue 2;
92
				}
93
			}
94
95
			if ($returnFirstMatchingOperationOnly) {
96
				return $operation;
97
			}
98
			$matches[] = $operation;
99
		}
100
101
		return $matches;
102
	}
103
104
	/**
105
	 * @param array $check
106
	 * @return bool
107
	 */
108
	protected function check(array $check) {
109
		try {
110
			$checkInstance = $this->container->query($check['class']);
0 ignored issues
show
Bug introduced by
The method query does only exist in OC\Server, but not in OCP\IServerContainer.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
111
		} catch (QueryException $e) {
112
			// Check does not exist, assume it matches.
113
			return true;
114
		}
115
116
		if ($checkInstance instanceof ICheck) {
117
			$checkInstance->setFileInfo($this->storage, $this->path);
118
			return $checkInstance->executeCheck($check['operator'], $check['value']);
119
		} else {
120
			// Check is invalid
121
			throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class']));
122
		}
123
	}
124
125
	/**
126
	 * @param string $class
127
	 * @return array[]
128
	 */
129
	public function getOperations($class) {
130
		if (isset($this->operations[$class])) {
131
			return $this->operations[$class];
132
		}
133
134
		$query = $this->connection->getQueryBuilder();
135
136
		$query->select('*')
137
			->from('flow_operations')
138
			->where($query->expr()->eq('class', $query->createNamedParameter($class)));
139
		$result = $query->execute();
140
141
		$this->operations[$class] = [];
142
		while ($row = $result->fetch()) {
143
			$this->operations[$class][] = $row;
144
		}
145
		$result->closeCursor();
146
147
		return $this->operations[$class];
148
	}
149
150
	/**
151
	 * @param int $id
152
	 * @return array
153
	 * @throws \UnexpectedValueException
154
	 */
155 View Code Duplication
	protected function getOperation($id) {
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...
156
		$query = $this->connection->getQueryBuilder();
157
		$query->select('*')
158
			->from('flow_operations')
159
			->where($query->expr()->eq('id', $query->createNamedParameter($id)));
160
		$result = $query->execute();
161
		$row = $result->fetch();
162
		$result->closeCursor();
163
164
		if ($row) {
165
			return $row;
166
		}
167
168
		throw new \UnexpectedValueException($this->l->t('Operation #%s does not exist', $id));
0 ignored issues
show
Documentation introduced by
$id is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
169
	}
170
171
	/**
172
	 * @param string $class
173
	 * @param string $name
174
	 * @param array[] $checks
175
	 * @param string $operation
176
	 * @return array The added operation
177
	 * @throws \UnexpectedValueException
178
	 */
179
	public function addOperation($class, $name, array $checks, $operation) {
180
		$this->validateOperation($class, $name, $checks, $operation);
181
182
		$checkIds = [];
183 View Code Duplication
		foreach ($checks as $check) {
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...
184
			$checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']);
185
		}
186
187
		$query = $this->connection->getQueryBuilder();
188
		$query->insert('flow_operations')
189
			->values([
190
				'class' => $query->createNamedParameter($class),
191
				'name' => $query->createNamedParameter($name),
192
				'checks' => $query->createNamedParameter(json_encode(array_unique($checkIds))),
193
				'operation' => $query->createNamedParameter($operation),
194
			]);
195
		$query->execute();
196
197
		$id = $query->getLastInsertId();
198
		return $this->getOperation($id);
199
	}
200
201
	/**
202
	 * @param int $id
203
	 * @param string $name
204
	 * @param array[] $checks
205
	 * @param string $operation
206
	 * @return array The updated operation
207
	 * @throws \UnexpectedValueException
208
	 */
209
	public function updateOperation($id, $name, array $checks, $operation) {
210
		$row = $this->getOperation($id);
211
		$this->validateOperation($row['class'], $name, $checks, $operation);
212
213
		$checkIds = [];
214 View Code Duplication
		foreach ($checks as $check) {
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...
215
			$checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']);
216
		}
217
218
		$query = $this->connection->getQueryBuilder();
219
		$query->update('flow_operations')
220
			->set('name', $query->createNamedParameter($name))
221
			->set('checks', $query->createNamedParameter(json_encode(array_unique($checkIds))))
222
			->set('operation', $query->createNamedParameter($operation))
223
			->where($query->expr()->eq('id', $query->createNamedParameter($id)));
224
		$query->execute();
225
226
		return $this->getOperation($id);
227
	}
228
229
	/**
230
	 * @param int $id
231
	 * @return bool
232
	 * @throws \UnexpectedValueException
233
	 */
234
	public function deleteOperation($id) {
235
		$query = $this->connection->getQueryBuilder();
236
		$query->delete('flow_operations')
237
			->where($query->expr()->eq('id', $query->createNamedParameter($id)));
238
		return (bool) $query->execute();
239
	}
240
241
	/**
242
	 * @param string $class
243
	 * @param string $name
244
	 * @param array[] $checks
245
	 * @param string $operation
246
	 * @throws \UnexpectedValueException
247
	 */
248
	protected function validateOperation($class, $name, array $checks, $operation) {
249
		try {
250
			/** @var IOperation $instance */
251
			$instance = $this->container->query($class);
0 ignored issues
show
Bug introduced by
The method query does only exist in OC\Server, but not in OCP\IServerContainer.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
252
		} catch (QueryException $e) {
253
			throw new \UnexpectedValueException($this->l->t('Operation %s does not exist', $class));
0 ignored issues
show
Documentation introduced by
$class is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
254
		}
255
256
		if (!($instance instanceof IOperation)) {
257
			throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', $class));
0 ignored issues
show
Documentation introduced by
$class is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
258
		}
259
260
		$instance->validateOperation($name, $checks, $operation);
261
262
		foreach ($checks as $check) {
263
			try {
264
				/** @var ICheck $instance */
265
				$instance = $this->container->query($check['class']);
266
			} catch (QueryException $e) {
267
				throw new \UnexpectedValueException($this->l->t('Check %s does not exist', $class));
0 ignored issues
show
Documentation introduced by
$class is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
268
			}
269
270
			if (!($instance instanceof ICheck)) {
271
				throw new \UnexpectedValueException($this->l->t('Check %s is invalid', $class));
0 ignored issues
show
Documentation introduced by
$class is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
272
			}
273
274
			$instance->validateCheck($check['operator'], $check['value']);
275
		}
276
	}
277
278
	/**
279
	 * @param int[] $checkIds
280
	 * @return array[]
281
	 */
282
	public function getChecks(array $checkIds) {
283
		$checkIds = array_map('intval', $checkIds);
284
285
		$checks = [];
286
		foreach ($checkIds as $i => $checkId) {
287
			if (isset($this->checks[$checkId])) {
288
				$checks[$checkId] = $this->checks[$checkId];
289
				unset($checkIds[$i]);
290
			}
291
		}
292
293
		if (empty($checkIds)) {
294
			return $checks;
295
		}
296
297
		$query = $this->connection->getQueryBuilder();
298
		$query->select('*')
299
			->from('flow_checks')
300
			->where($query->expr()->in('id', $query->createNamedParameter($checkIds, IQueryBuilder::PARAM_INT_ARRAY)));
301
		$result = $query->execute();
302
303
		$checks = [];
304
		while ($row = $result->fetch()) {
305
			$this->checks[(int) $row['id']] = $row;
306
			$checks[(int) $row['id']] = $row;
307
		}
308
		$result->closeCursor();
309
310
		$checkIds = array_diff($checkIds, array_keys($checks));
311
312
		if (!empty($checkIds)) {
313
			$missingCheck = array_pop($checkIds);
314
			throw new \UnexpectedValueException($this->l->t('Check #%s does not exist', $missingCheck));
315
		}
316
317
		return $checks;
318
	}
319
320
	/**
321
	 * @param string $class
322
	 * @param string $operator
323
	 * @param string $value
324
	 * @return int Check unique ID
325
	 */
326
	protected function addCheck($class, $operator, $value) {
327
		$hash = md5($class . '::' . $operator . '::' . $value);
328
329
		$query = $this->connection->getQueryBuilder();
330
		$query->select('id')
331
			->from('flow_checks')
332
			->where($query->expr()->eq('hash', $query->createNamedParameter($hash)));
333
		$result = $query->execute();
334
335
		if ($row = $result->fetch()) {
336
			$result->closeCursor();
337
			return (int) $row['id'];
338
		}
339
340
		$query = $this->connection->getQueryBuilder();
341
		$query->insert('flow_checks')
342
			->values([
343
				'class' => $query->createNamedParameter($class),
344
				'operator' => $query->createNamedParameter($operator),
345
				'value' => $query->createNamedParameter($value),
346
				'hash' => $query->createNamedParameter($hash),
347
			]);
348
		$query->execute();
349
350
		return $query->getLastInsertId();
351
	}
352
}
353