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

DataBaseDumpManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dump() 0 10 2
A load() 0 4 1
A runBin() 0 7 1
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