Completed
Push — master ( 4fd324...91fc25 )
by Morris
26:54 queued 13:36
created

AsyncBus::canRunAsync()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Robin Appelman <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OC\Command;
24
25
use OCP\Command\IBus;
26
use OCP\Command\ICommand;
27
28
/**
29
 * Asynchronous command bus that uses the background job system as backend
30
 */
31
abstract class AsyncBus implements IBus {
32
	/**
33
	 * List of traits for command which require sync execution
34
	 *
35
	 * @var string[]
36
	 */
37
	private $syncTraits = [];
38
39
	/**
40
	 * Schedule a command to be fired
41
	 *
42
	 * @param \OCP\Command\ICommand | callable $command
43
	 */
44
	public function push($command) {
45
		if ($this->canRunAsync($command)) {
46
			$this->queueCommand($command);
47
		} else {
48
			$this->runCommand($command);
49
		}
50
	}
51
52
	/**
53
	 * Queue a command in the bus
54
	 *
55
	 * @param \OCP\Command\ICommand | callable $command
56
	 */
57
	abstract protected function queueCommand($command);
58
59
	/**
60
	 * Require all commands using a trait to be run synchronous
61
	 *
62
	 * @param string $trait
63
	 */
64
	public function requireSync($trait) {
65
		$this->syncTraits[] = trim($trait, '\\');
66
	}
67
68
	/**
69
	 * @param \OCP\Command\ICommand | callable $command
70
	 */
71
	private function runCommand($command) {
72
		if ($command instanceof ICommand) {
73
			$command->handle();
74
		} else {
75
			$command();
76
		}
77
	}
78
79
	/**
80
	 * @param \OCP\Command\ICommand | callable $command
81
	 * @return bool
82
	 */
83
	private function canRunAsync($command) {
84
		$traits = $this->getTraits($command);
85
		foreach ($traits as $trait) {
86
			if (array_search($trait, $this->syncTraits) !== false) {
87
				return false;
88
			}
89
		}
90
		return true;
91
	}
92
93
	/**
94
	 * @param \OCP\Command\ICommand | callable $command
95
	 * @return string[]
96
	 */
97
	private function getTraits($command) {
98
		if ($command instanceof ICommand) {
99
			return class_uses($command);
100
		} else {
101
			return [];
102
		}
103
	}
104
}
105