SettingConfigHandler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 20.24 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 17
loc 84
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
C execute() 17 61 13

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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