DatabaseConfigHandler::execute()   C
last analyzed

Complexity

Conditions 13
Paths 63

Size

Total Lines 74
Code Lines 39

Duplication

Lines 5
Ratio 6.76 %

Importance

Changes 0
Metric Value
cc 13
eloc 39
nc 63
nop 1
dl 5
loc 74
rs 5.3888
c 0
b 0
f 0

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
// +---------------------------------------------------------------------------+
4
// | This file is part of the Agavi package.                                   |
5
// | Copyright (c) 2005-2011 the Agavi Project.                                |
6
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr.    |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
17
namespace Agavi\Config;
18
19
use Agavi\Config\Util\Dom\XmlConfigDomDocument;
20
use Agavi\Config\Util\Dom\XmlConfigDomElement;
21
use Agavi\Exception\ConfigurationException;
22
use Agavi\Exception\ParseException;
23
24
/**
25
 * DatabaseConfigHandler allows you to setup database connections in a
26
 * configuration file that will be created for you automatically upon first
27
 * request.
28
 *
29
 * @package    agavi
30
 * @subpackage config
31
 *
32
 * @author     Sean Kerr <[email protected]>
33
 * @author     Dominik del Bondio <[email protected]>
34
 * @author     Noah Fontes <[email protected]>
35
 * @copyright  Authors
36
 * @copyright  The Agavi Project
37
 *
38
 * @since      0.9.0
39
 *
40
 * @version    $Id$
41
 */
42
class DatabaseConfigHandler extends XmlConfigHandler
43
{
44
    const XML_NAMESPACE = 'http://agavi.org/agavi/config/parts/databases/1.1';
45
    
46
    /**
47
     * Execute this configuration handler.
48
     *
49
     * @param      XmlConfigDomDocument $document The document to parse.
50
     *
51
     * @return     string Data to be written to a cache file.
52
     *
53
     * @throws     <b>ParseException</b> If a requested configuration file is
54
     *                                   improperly formatted.
55
     *
56
     * @author     Sean Kerr <[email protected]>
57
     * @author     Dominik del Bondio <[email protected]>
58
     * @author     Noah Fontes <[email protected]>
59
     * @since      0.9.0
60
     */
61
    public function execute(XmlConfigDomDocument $document)
62
    {
63
        // set up our default namespace
64
        $document->setDefaultNamespace(self::XML_NAMESPACE, 'databases');
65
        
66
        $databases = array();
67
        $default = null;
68
        foreach ($document->getConfigurationElements() as $configuration) {
69
            if (!$configuration->hasChildren('databases')) {
70
                continue;
71
            }
72
            
73
            $databasesElement = $configuration->getChild('databases');
74
            
75
            // make sure we have a default database exists
76
            if (!$databasesElement->hasAttribute('default') && $default === null) {
77
                // missing default database
78
                $error = 'Configuration file "%s" must specify a default database configuration';
79
                $error = sprintf($error, $document->documentURI);
80
81
                throw new ParseException($error);
82
            }
83
            if ($databasesElement->hasAttribute('default')) {
84
                $default = $databasesElement->getAttribute('default');
85
            }
86
87
            // let's do our fancy work
88
            /** @var XmlConfigDomElement $database */
89
            foreach ($configuration->get('databases') as $database) {
90
                $name = $database->getAttribute('name');
91
92
                if (!isset($databases[$name])) {
93
                    $databases[$name] = array('parameters' => array());
94
95
                    if (!$database->hasAttribute('class')) {
96
                        $error = 'Configuration file "%s" specifies database "%s" with missing class key';
97
                        $error = sprintf($error, $document->documentURI, $name);
98
99
                        throw new ParseException($error);
100
                    }
101
                }
102
103
                $databases[$name]['class'] = $database->hasAttribute('class') ? $database->getAttribute('class') : $databases[$name]['class'];
104
105
                $databases[$name]['parameters'] = $database->getAgaviParameters($databases[$name]['parameters']);
106
            }
107
        }
108
109
        if (!$databases) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $databases of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
110
            // we have no connections
111
            $error = 'Configuration file "%s" does not contain any database connections.';
112
            $error = sprintf($error, $document->documentURI);
113
            throw new ConfigurationException($error);
114
        }
115
116
        $data = array();
117
118
        foreach ($databases as $name => $db) {
119
            // append new data
120
            $data[] = sprintf('$database = new %s();', $db['class']);
121
            $data[] = sprintf('$this->databases[%s] = $database;', var_export($name, true));
122
            $data[] = sprintf('$database->initialize($this, %s);', var_export($db['parameters'], true));
123
        }
124
125 View Code Duplication
        if (!isset($databases[$default])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
            $error = 'Configuration file "%s" specifies undefined default database "%s".';
127
            $error = sprintf($error, $document->documentURI, $default);
128
            throw new ConfigurationException($error);
129
        }
130
131
        $data[] = sprintf('$this->defaultDatabaseName = %s;', var_export($default, true));
132
133
        return $this->generate($data, $document->documentURI);
134
    }
135
}
136