Passed
Push — master ( 0b0084...eb6b18 )
by Milan
01:28
created

Builder::createSourceFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace PmgDev\DatabaseReplicator;
4
5
use Nette\Utils\FileSystem;
6
use PmgDev\DatabaseReplicator\Source;
7
use PmgDev\DatabaseReplicator\Source\Files;
8
9
abstract class Builder
10
{
11
	/** @var string */
12
	private $sourceFile;
13
14
	/** @var Config */
15
	private $config;
16
17
	/** @var Command */
18
	private $command;
19
20
	/** @var string */
21
	private $tempDir = '/tmp';
22
23
24
	public function __construct(string $sourceFile, Config $config, Command $command)
25
	{
26
		$this->sourceFile = $sourceFile;
27
		$this->config = $config;
28
		$this->command = $command;
29
	}
30
31
32
	final public function setTempDir(string $tempDir): void
33
	{
34
		FileSystem::createDir($tempDir);
35
		$this->tempDir = $tempDir;
36
	}
37
38
39
	final public function createDatabase(): Database
40
	{
41
		return new Database($this->createConnectionFactory());
42
	}
43
44
45
	abstract protected function createConnectionFactory(): ConnectionFactory;
46
47
48
	final protected function createDatabaseReplicator(
49
		?Files $files = NULL,
50
		?Files $dynamicFiles = NULL
51
	): Database\Replicator
52
	{
53
		$sourceHash = $this->createSourceHash($files);
54
		$databasePrefix = $this->createDatabasePrefix($sourceHash);
55
		$sourceDatabase = $this->createSourceDatabase($databasePrefix, $sourceHash);
56
		return new Database\Replicator($this->command, $databasePrefix, $sourceDatabase, $dynamicFiles);
57
	}
58
59
60
	final protected function createSourceHash(?Files $files = NULL): Source\Hash
61
	{
62
		if ($files === NULL) {
63
			$files = $this->createSourceFiles();
64
		}
65
		return new Source\Hash($this->config->database, $this->tempDir, $files);
66
	}
67
68
69
	final protected function createSourceFiles()
70
	{
71
		return new Files([$this->sourceFile]);
72
	}
73
74
75
	final protected function createDatabasePrefix(Source\Hash $sourceHash): Database\Prefix
76
	{
77
		return new Database\Prefix($this->getConfig(), $sourceHash);
78
	}
79
80
81
	final protected function getConfig(): Config
82
	{
83
		return clone $this->config;
84
	}
85
86
87
	final protected function createSourceDatabase(
88
		Database\Prefix $prefix,
89
		Source\Hash $sourceHash
90
	): Source\Database
91
	{
92
		return new Source\Database($prefix, $sourceHash, $this->command);
93
	}
94
95
96
	final protected function getCommand(): Command
97
	{
98
		return $this->command;
99
	}
100
101
}
102