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

CronBus::serializeCommand()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 3
dl 0
loc 10
c 0
b 0
f 0
cc 4
eloc 8
nop 1
rs 9.2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Robin Appelman <[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 OC\Command;
23
24
use OCP\Command\ICommand;
25
use SuperClosure\Serializer;
26
27
class CronBus extends AsyncBus {
28
	/**
29
	 * @var \OCP\BackgroundJob\IJobList
30
	 */
31
	private $jobList;
32
33
34
	/**
35
	 * @param \OCP\BackgroundJob\IJobList $jobList
36
	 */
37
	public function __construct($jobList) {
38
		$this->jobList = $jobList;
39
	}
40
41
	protected function queueCommand($command) {
42
		$this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
43
	}
44
45
	/**
46
	 * @param \OCP\Command\ICommand | callable $command
47
	 * @return string
48
	 */
49
	private function getJobClass($command) {
50
		if ($command instanceof \Closure) {
51
			return 'OC\Command\ClosureJob';
52
		} else if (is_callable($command)) {
53
			return 'OC\Command\CallableJob';
54
		} else if ($command instanceof ICommand) {
55
			return 'OC\Command\CommandJob';
56
		} else {
57
			throw new \InvalidArgumentException('Invalid command');
58
		}
59
	}
60
61
	/**
62
	 * @param \OCP\Command\ICommand | callable $command
63
	 * @return string
64
	 */
65
	private function serializeCommand($command) {
66
		if ($command instanceof \Closure) {
67
			$serializer = new Serializer();
68
			return $serializer->serialize($command);
69
		} else if (is_callable($command) or $command instanceof ICommand) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
70
			return serialize($command);
71
		} else {
72
			throw new \InvalidArgumentException('Invalid command');
73
		}
74
	}
75
}
76