SettingConfigHandler::execute()   C
last analyzed

Complexity

Conditions 13
Paths 76

Size

Total Lines 61
Code Lines 32

Duplication

Lines 17
Ratio 27.87 %

Importance

Changes 0
Metric Value
cc 13
eloc 32
nc 76
nop 1
dl 17
loc 61
rs 6.2659
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\Util\Toolkit;
23
24
/**
25
 * SettingConfigHandler handles the settings.xml file
26
 *
27
 * @package    agavi
28
 * @subpackage config
29
 *
30
 * @author     David Zülke <[email protected]>
31
 * @author     Dominik del Bondio <[email protected]>
32
 * @author     Sean Kerr <[email protected]>
33
 * @copyright  Authors
34
 * @copyright  The Agavi Project
35
 *
36
 * @since      0.9.0
37
 *
38
 * @version    $Id$
39
 */
40
class SettingConfigHandler extends XmlConfigHandler
41
{
42
    const XML_NAMESPACE = 'http://agavi.org/agavi/config/parts/settings/1.1';
43
    
44
    /**
45
     * Execute this configuration handler.
46
     *
47
     * @param      XmlConfigDomDocument $document The document to parse.
48
     *
49
     * @return     string Data to be written to a cache file.
50
     *
51
     * @throws     <b>ConfigurationException</b> If a requested configuration
52
     *                                           file does not exist or is not
53
     *                                           readable.
54
     * @throws     <b>ParseException</b> If a requested configuration file is
55
     *                                   improperly formatted.
56
     *
57
     * @author     David Zülke <[email protected]>
58
     * @author     Dominik del Bondio <[email protected]>
59
     * @author     Sean Kerr <[email protected]>
60
     * @since      0.9.0
61
     */
62
    public function execute(XmlConfigDomDocument $document)
63
    {
64
        // set up our default namespace
65
        $document->setDefaultNamespace(self::XML_NAMESPACE, 'settings');
66
        
67
        // init our data array
68
        $data = array();
69
        
70
        $prefix = 'core.';
71
        
72
        foreach ($document->getConfigurationElements() as $cfg) {
73
            // let's do our fancy work
74
            if ($cfg->has('system_controllers')) {
75
                foreach ($cfg->get('system_controllers') as $controller) {
76
                    $name = $controller->getAttribute('name');
77
                    $data[sprintf('controllers.%s_module', $name)] = $controller->getChild('module')->getValue();
78
                    $data[sprintf('controllers.%s_controller', $name)] = $controller->getChild('controller')->getValue();
79
                }
80
            }
81
            
82
            // loop over <setting> elements; there can be many of them
83
            /** @var XmlConfigDomElement $setting */
84 View Code Duplication
            foreach ($cfg->get('settings') as $setting) {
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...
85
                $localPrefix = $prefix;
86
                
87
                // let's see if this buddy has a <settings> parent with valuable information
88
                if ($setting->parentNode->localName == 'settings') {
89
                    if ($setting->parentNode->hasAttribute('prefix')) {
90
                        $localPrefix = $setting->parentNode->getAttribute('prefix');
91
                    }
92
                }
93
                
94
                $settingName = $localPrefix . $setting->getAttribute('name');
95
                if ($setting->hasAgaviParameters()) {
96
                    $data[$settingName] = $setting->getAgaviParameters();
97
                } else {
98
                    $data[$settingName] = $setting->getLiteralValue();
99
                }
100
            }
101
            
102
            if ($cfg->has('exception_templates')) {
103
                foreach ($cfg->get('exception_templates') as $exception_template) {
104
                    $tpl = Toolkit::expandDirectives($exception_template->getValue());
105
                    if (!is_readable($tpl)) {
106
                        throw new ConfigurationException('Exception template "' . $tpl . '" does not exist or is unreadable');
107
                    }
108
                    if ($exception_template->hasAttribute('context')) {
109
                        foreach (array_map('trim', explode(' ', $exception_template->getAttribute('context'))) as $ctx) {
110
                            $data['exception.templates.' . $ctx] = $tpl;
111
                        }
112
                    } else {
113
                        $data['exception.default_template'] = Toolkit::expandDirectives($tpl);
114
                    }
115
                }
116
            }
117
        }
118
119
        $code = 'Agavi\\Config\\Config::fromArray(' . var_export($data, true) . ');';
120
121
        return $this->generate($code, $document->documentURI);
122
    }
123
}
124