Passed
Push — master ( 5b90a0...8ca5e5 )
by Webnet
08:05
created

AnonymizeCommandTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigFromFile() 0 11 1
B getConnectionFromInput() 0 21 7
A getConnection() 0 5 1
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 InputInterface $input
23
     *
24
     * @throws \Doctrine\DBAL\DBALException
25
     *
26
     * @return Connection|null
27
     */
28
    protected function getConnectionFromInput(InputInterface $input)
29
    {
30
        if ($dbURL = $input->getOption('url')) {
31
            return $this->getConnection(['url' => $dbURL]);
32
        } elseif (($type = $input->getOption('type'))
33
            && ($host = $input->getOption('host'))
34
            && ($port = $input->getOption('port'))
35
            && ($database = $input->getOption('database'))
36
            && ($user = $input->getOption('user'))
37
        ) {
38
            return $this->getConnection([
39
                'driver' => $type,
40
                'host' => $host,
41
                'port' => $port,
42
                'dbname' => $database,
43
                'user' => $user,
44
                'password' => $input->getOption('password'),
45
            ]);
46
        }
47
48
        return null;
49
    }
50
51
    /**
52
     * @param array $params
53
     *
54
     * @throws \Doctrine\DBAL\DBALException
55
     *
56
     * @return Connection
57
     */
58
    protected function getConnection(array $params): Connection
59
    {
60
        $config = new DoctrineDBALConfiguration();
61
62
        return DriverManager::getConnection($params, $config);
63
    }
64
65
    /**
66
     * @param string $configFilePath
67
     *
68
     * @return array
69
     */
70
    protected function getConfigFromFile(string $configFilePath)
71
    {
72
        $fileLocator = new FileLocator();
73
        $loaderResolver = new LoaderResolver([new YamlAnonymizerLoader($fileLocator)]);
74
        $delegatingLoader = new DelegatingLoader($loaderResolver);
75
        $rawConfig = $delegatingLoader->load($configFilePath);
76
77
        $configuration = new Configuration();
78
        $processor = new Processor();
79
80
        return $processor->processConfiguration($configuration, $rawConfig);
81
    }
82
}
83