GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( d274d7...b4c702 )
by
unknown
10:03 queued 17s
created

Config::parse()   B

Complexity

Conditions 10
Paths 28

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 10
eloc 31
nc 28
nop 0
dl 0
loc 51
rs 7.6666
c 6
b 0
f 0
ccs 0
cts 44
cp 0
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Graze\Morphism;
4
5
use Doctrine\DBAL\Configuration;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\DriverManager;
9
use RuntimeException;
10
use Symfony\Component\Yaml\Parser;
11
12
/**
13
 * A parser for config files
14
 */
15
class Config
16
{
17
    /** @var array */
18
    private $entries = [];
19
    /** @var string */
20
    private $path;
21
22
    /**
23
     * Constructor
24
     *
25
     * @param string $path Path to config file.
26
     */
27
    public function __construct($path)
28
    {
29
        $this->path = $path;
30
    }
31
32
    /**
33
     * Parse the config file provided in the constructor.
34
     */
35
    public function parse()
36
    {
37
        if (!file_exists($this->path)) {
38
            throw new RuntimeException("Specified config file does not exist: {$this->path}");
39
        }
40
41
        $parser = new Parser();
42
        $config = $parser->parse(file_get_contents($this->path));
43
44
        if (!isset($config['databases'])) {
45
            throw new RuntimeException("Missing databases section in '{$this->path}'");
46
        }
47
48
        $entries = [];
49
        foreach ($config['databases'] as $connectionName => $entry) {
50
            if (empty($entry['morphism']['enable'])) {
51
                continue;
52
            }
53
            $morphism = $entry['morphism'];
54
            unset($entry['morphism']);
55
56
            if (!isset($entry['dbname'])) {
57
                $entry['dbname'] = $connectionName;
58
            }
59
60
            $matchTables = [];
61
            foreach (['include', 'exclude'] as $key) {
62
                $regex = '';
63
                if (!empty($morphism["{$key}"])) {
64
                    $regex =  '/^(' . implode('|', $morphism["{$key}"]) . ')$/';
65
                }
66
                $matchTables[$key] = $regex;
67
            }
68
69
            $schemaDefinitionPaths = $morphism['schemaDefinitionPath'];
70
            if (!$schemaDefinitionPaths) {
71
                $schemaDefinitionPaths = 'schema/'.$connectionName;
72
            }
73
            if (!is_array($schemaDefinitionPaths)) {
74
                $schemaDefinitionPaths = [ $schemaDefinitionPaths ];
75
            }
76
77
            $entries[$connectionName] = [
78
                'connection' => $entry,
79
                'morphism'   => [
80
                    'matchTables' => $matchTables,
81
                    'schemaDefinitionPath' => $schemaDefinitionPaths,
82
                ],
83
            ];
84
        }
85
        $this->entries = $entries;
86
    }
87
88
    /**
89
     * Returns the parameters to open the named database connection, suitable
90
     * for passing to \Doctrine\DBAL\DriverManager::getConnection().
91
     *
92
     * Some subset of these parameters will be returned:
93
     *
94
     *      'driver'      => $pdo_driver
95
     *      'dbname'      => $dbname
96
     *      'user'        => $user
97
     *      'password'    => $password
98
     *      'host'        => $host
99
     *      'port'        => $port
100
     *      'unix_socket' => $socket
101
     *
102
     * @param string $connectionName Name of the connection to look up
103
     * @return array ['connection' => [$param => $value, ...], 'morphism' => [ ... ] ]
104
     */
105
    public function getEntry($connectionName)
106
    {
107
        if (!isset($this->entries[$connectionName])) {
108
            throw new RuntimeException("Unknown connection '$connectionName'");
109
        }
110
111
        return $this->entries[$connectionName];
112
    }
113
114
    /**
115
     * Returns a new database connection using the parameters named by
116
     * connectionName.
117
     *
118
     * @param string $connectionName
119
     * @return Connection
120
     * @throws DBALException
121
     */
122
    public function getConnection($connectionName)
123
    {
124
        $entry = $this->getEntry($connectionName);
125
        $dbalConfig = new Configuration();
126
        return DriverManager::getConnection($entry['connection'], $dbalConfig);
127
    }
128
129
    /**
130
     * Returns the names of all the connections specified in the config file.
131
     *
132
     * @return string[]
133
     */
134
    public function getConnectionNames()
135
    {
136
        return array_keys($this->entries);
137
    }
138
}
139