Dumper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 56
ccs 18
cts 18
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dump() 0 19 4
A __construct() 0 10 1
1
<?php
2
/**
3
 * This file is part of graze/sprout.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/sprout/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/sprout
12
 */
13
14
namespace Graze\Sprout\Dump;
15
16
use Graze\Sprout\Config\SchemaConfigInterface;
17
use League\Flysystem\AdapterInterface;
18
use League\Flysystem\Config;
19
use RuntimeException;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
class Dumper
23
{
24
    /** @var SchemaConfigInterface */
25
    private $schemaConfig;
26
    /** @var OutputInterface */
27
    private $output;
28
    /** @var TableDumperFactory */
29
    private $factory;
30
    /** @var AdapterInterface */
31
    private $filesystem;
32
33
    /**
34
     * Dumper constructor.
35
     *
36
     * @param SchemaConfigInterface $schemaConfig
37
     * @param OutputInterface       $output
38
     * @param TableDumperFactory    $factory
39
     * @param AdapterInterface      $filesystem
40
     */
41 5
    public function __construct(
42
        SchemaConfigInterface $schemaConfig,
43
        OutputInterface $output,
44
        TableDumperFactory $factory,
45
        AdapterInterface $filesystem
46
    ) {
47 5
        $this->schemaConfig = $schemaConfig;
48 5
        $this->output = $output;
49 5
        $this->factory = $factory;
50 5
        $this->filesystem = $filesystem;
51 5
    }
52
53
    /**
54
     * Dump a collection of tables to disk
55
     *
56
     * @param string   $path
57
     * @param string[] $tables
58
     */
59 5
    public function dump(string $path, array $tables = [])
60
    {
61 5
        $tables = array_unique($tables);
62
63 5
        if (count($tables) === 0) {
64 1
            $this->output->writeln('<warning>No tables specified, nothing to do</warning>');
65 1
            return;
66
        }
67
68 4
        if ($this->filesystem->createDir($path, new Config()) === false) {
69 1
            throw new RuntimeException('dump: failed to create directory: ' . $path);
70
        }
71
72 3
        $tableDumper = $this->factory->getDumper($this->schemaConfig->getConnection());
73 3
        $schema = $this->schemaConfig->getSchema();
74
75 3
        foreach ($tables as $table) {
76 3
            $file = sprintf('%s/%s.sql', $path, $table);
77 3
            $tableDumper->dump($schema, $table, $file);
78
        }
79 3
    }
80
}
81