Completed
Push — master ( 84470e...c291b9 )
by Ron
03:30
created

MySQLBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A useLocking() 0 4 1
A setLockPrefix() 0 4 1
A build() 0 7 1
1
<?php
2
namespace Kir\Services\Cmd\Dispatcher\Builder;
3
4
use Kir\Services\Cmd\Dispatcher\AttributeRepositories\MySQLAttributeRepository;
5
use Kir\Services\Cmd\Dispatcher\Dispatcher;
6
use Kir\Services\Cmd\Dispatcher\Dispatchers\DefaultDispatcher;
7
use PDO;
8
9
class MySQLBuilder {
10
	/** @var PDO */
11
	private $pdo;
12
	/** @var bool */
13
	private $useLocking = true;
14
	/** @var string */
15
	private $lockPrefix = '';
16
	
17
	/**
18
	 * @param PDO $pdo
19
	 */
20
	public function __construct(PDO $pdo) {
21
		$this->pdo = $pdo;
22
	}
23
	
24
	/**
25
	 * Enabled by default.
26
	 * You can disable global locking (using MySQL's GET_LOCK) per service-run.
27
	 * A Lock will be optained before a service runs and will be released, is the service is done
28
	 * (or an exception was thrown during the run)
29
	 *
30
	 * @param bool $useLocking
31
	 * @return $this
32
	 */
33
	public function useLocking(bool $useLocking = true): self {
34
		$this->useLocking = $useLocking;
35
		return $this;
36
	}
37
	
38
	/**
39
	 * Set the lock-prefix for the MySQL-`GET_LOCK` name.
40
	 *
41
	 * You can think of it as calling the function like this:
42
	 * `SELECT GET_LOCK(CONCAT(<lock-prefix>, <registered-service-name>), 0)`
43
	 *
44
	 * Something like `my-app:` would result in `my-app:service-xy` if the particular service was registered as
45
	 * `service-xy`. The Lock is per service. Between two services, the lock will be released and reoptained.
46
	 *
47
	 * @param string $lockPrefix
48
	 * @return $this
49
	 */
50
	public function setLockPrefix(string $lockPrefix = ''): self {
51
		$this->lockPrefix = $lockPrefix;
52
		return $this;
53
	}
54
	
55
	/**
56
	 * @return DefaultDispatcher
57
	 */
58
	public function build(): DefaultDispatcher {
59
		$repos = new MySQLAttributeRepository($this->pdo, [
0 ignored issues
show
Documentation introduced by
array('use-locking' => $...' => $this->lockPrefix) is of type array<string,boolean|str...lock-prefix":"string"}>, but the function expects a string.

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...
60
			'use-locking' => $this->useLocking,
61
			'lock-prefix' => $this->lockPrefix,
62
		]);
63
		return new DefaultDispatcher($repos);
64
	}
65
}