Passed
Push — master ( 265c0b...e337ae )
by Webnet
01:48
created

AnonymizeCommandTrait::getConnectionFromInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace WebnetFr\DatabaseAnonymizer\Command;
4
5
use Doctrine\DBAL\Configuration as DoctrineDBALConfiguration;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DriverManager;
8
use Symfony\Component\Config\Definition\Processor;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\Config\Loader\DelegatingLoader;
11
use Symfony\Component\Config\Loader\LoaderResolver;
12
use Symfony\Component\Console\Input\InputInterface;
13
use WebnetFr\DatabaseAnonymizer\Config\Configuration;
14
use WebnetFr\DatabaseAnonymizer\Config\YamlAnonymizerLoader;
15
16
/**
17
 * @author Vlad Riabchenko <[email protected]>
18
 */
19
trait AnonymizeCommandTrait
20
{
21
    /**
22
     * @param array $params
23
     *
24
     * @throws \Doctrine\DBAL\DBALException
25
     *
26
     * @return Connection
27
     */
28
    protected function getConnectionFromInput(InputInterface $input)
29
    {
30
        if ($dbURL = $input->getOption('url')) {
31
            $params = ['url' => $dbURL];
32
        } else {
33
            $params = [
34
                'driver' => $input->getOption('type'),
35
                'host' => $input->getOption('host'),
36
                'port' => $input->getOption('port'),
37
                'dbname' => $input->getOption('database'),
38
                'user' => $input->getOption('user'),
39
                'password' => $input->getOption('password'),
40
            ];
41
        }
42
43
        return $this->getConnection($params);
44
    }
45
46
    /**
47
     * @param array $params
48
     *
49
     * @throws \Doctrine\DBAL\DBALException
50
     *
51
     * @return Connection
52
     */
53
    protected function getConnection(array $params): Connection
54
    {
55
        $config = new DoctrineDBALConfiguration();
56
57
        return DriverManager::getConnection($params, $config);
58
    }
59
60
    /**
61
     * @param string $configFilePath
62
     *
63
     * @return array
64
     */
65
    protected function getConfigFromFile(string $configFilePath)
66
    {
67
        $fileLocator = new FileLocator();
68
        $loaderResolver = new LoaderResolver([new YamlAnonymizerLoader($fileLocator)]);
69
        $delegatingLoader = new DelegatingLoader($loaderResolver);
70
        $rawConfig = $delegatingLoader->load($configFilePath);
71
72
        $configuration = new Configuration();
73
        $processor = new Processor();
74
75
        return $processor->processConfiguration($configuration, $rawConfig);
76
    }
77
}
78