Completed
Push — master ( 9e29b7...c3d8ab )
by Yaroslav
19:31
created

DataBaseDumpManager::dump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Utils;
4
5
use Symfony\Component\Process\Process;
6
7
class DataBaseDumpManager
8
{
9
    /**
10
     * @var string
11
     */
12
    private $rootDir;
13
14
    /**
15
     * @param string $rootDir
16
     */
17
    public function __construct($rootDir)
18
    {
19
        $this->rootDir = $rootDir;
20
    }
21
22
    /**
23
     * @return string|null
24
     */
25
    public function dump()
26
    {
27
        $output = $this->runBin('test-database-dump', uniqid('', true));
28
29
        if (preg_match('/ to \'(?<file>.+)\'$/', $output, $matches)) {
30
            return $matches['file'];
31
        }
32
33
        throw new \RuntimeException("Can't create database dump");
34
    }
35
36
    /**
37
     * @param $dumpFile
38
     */
39
    public function load($dumpFile)
40
    {
41
        $this->runBin('test-database-import', $dumpFile);
42
    }
43
44
    /**
45
     * @param string $bin
46
     * @param string $args
47
     * @return string
48
     */
49
    private function runBin($bin, $args)
50
    {
51
        $process = new Process(sprintf('%s/bin/%s %s', $this->rootDir, $bin, $args));
52
        $process->run();
53
54
        return $process->getOutput();
55
    }
56
}
57