Issues (2)

src/DatabaseOperation.php (1 issue)

1
<?php declare(strict_types=1);
2
3
namespace PmgDev\DatabaseReplicator;
4
5
use Nette\DI\Container;
6
use PmgDev\DatabaseReplicator\Source;
7
8
class DatabaseOperation
9
{
10
	/** @var Container */
11
	private $container;
12
13
	/** @var string */
14
	private $prefix = '';
15
16
	/** @var string[] */
17
	private $connectionsName = [];
18
19
20
	public function __construct(Container $container)
21
	{
22
		$this->container = $container;
23
	}
24
25
26
	final public function setPrefix(string $prefix): void
27
	{
28
		$this->prefix = $prefix;
29
	}
30
31
32
	/**
33
	 * @param string[] $connectionsName
34
	 */
35
	final public function setConnectionsName(array $connectionsName): void
36
	{
37
		$this->connectionsName = $connectionsName;
38
	}
39
40
41
	public function drop(string $database): void
42
	{
43
		$this->command()->drop($database);
44
	}
45
46
47
	/**
48
	 * @param bool|string[] $keepSourceDatabases
49
	 * @param callable|NULL $onAfterDrop
50
	 */
51
	public function dropAll($keepSourceDatabases = TRUE, ?callable $onAfterDrop = NULL): void
52
	{
53
		$this->run(function (string $name) use ($keepSourceDatabases, $onAfterDrop): void {
54
			$databases = $this->databaseReplicator($name)->clearDatabases($this->checkKeepDatabases($name, $keepSourceDatabases));
55
			if ($onAfterDrop !== NULL) {
56
				$onAfterDrop($name, $databases);
57
			}
58
			$this->sourceFile($name)->removeActiveFile();
59
		});
60
	}
61
62
63
	public function build(bool $isForce, ?callable $onStart = NULL, ?callable $onEnd = NULL): void
64
	{
65
		$this->run(function (string $name) use ($isForce, $onStart, $onEnd): void {
66
			if ($onStart !== NULL) {
67
				$onStart();
68
			}
69
			$sourceDatabase = $this->sourceDatabase($name);
70
			if ($isForce) {
71
				$this->databaseReplicator($name)->clearDatabases(FALSE);
72
			}
73
			$exists = $sourceDatabase->build();
74
			if ($onEnd !== NULL) {
75
				$prefix = $this->prefix($name);
76
				$onEnd($name, $exists, $prefix->config());
77
			}
78
		});
79
	}
80
81
82
	final protected function run(callable $function): void
83
	{
84
		foreach ($this->connectionsName as $name) {
85
			$function($name);
86
		}
87
	}
88
89
90
	final protected function command(): Command
91
	{
92
		return $this->getService('command');
93
	}
94
95
96
	final protected function databaseReplicator(string $name): Database\Replicator
97
	{
98
		return $this->getService("$name.database.replicator");
99
	}
100
101
102
	final protected function sourceDatabase(string $name): Source\Database
103
	{
104
		return $this->getService("$name.source.database");
105
	}
106
107
108
	final protected function sourceFile(string $name): Source\Hash
109
	{
110
		return $this->getService("$name.source.hash");
111
	}
112
113
114
	final protected function prefix(string $name): Database\Prefix
115
	{
116
		return $this->getService("$name.database.prefix");
117
	}
118
119
120
	final protected function getService(string $name)/*: object*/
121
	{
122
		return $this->container->getService($this->prefix . $name);
123
	}
124
125
126
	/**
127
	 * @param string $name
128
	 * @param string[]|bool $keepSourceDatabases
129
	 * @return string[]|bool
130
	 */
131
	protected function checkKeepDatabases(string $name, $keepSourceDatabases)
0 ignored issues
show
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

131
	protected function checkKeepDatabases(/** @scrutinizer ignore-unused */ string $name, $keepSourceDatabases)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
	{
133
		return $keepSourceDatabases;
134
	}
135
136
}
137