RbacDefinitionConfigHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 26.76 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 19
loc 71
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 19 19 3
B parseRoles() 0 20 5

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\XmlConfigDomElement;
20
use Agavi\Config\XmlConfigHandler;
21
use Agavi\Config\Util\Dom\XmlConfigDomDocument;
22
23
/**
24
 * RbacDefinitionConfigHandler handles RBAC role and permission definition files
25
 *
26
 * @package    agavi
27
 * @subpackage config
28
 *
29
 * @author     David Zülke <[email protected]>
30
 * @copyright  Authors
31
 * @copyright  The Agavi Project
32
 *
33
 * @since      0.11.0
34
 *
35
 * @version    $Id$
36
 */
37
class RbacDefinitionConfigHandler extends XmlConfigHandler
38
{
39
    const XML_NAMESPACE = 'http://agavi.org/agavi/config/parts/rbac_definitions/1.1';
40
    
41
    /**
42
     * Execute this configuration handler.
43
     *
44
     * @param      XmlConfigDomDocument $document The document to parse.
45
     *
46
     * @return     string Data to be written to a cache file.
47
     *
48
     * @throws     <b>UnreadableException</b> If a requested configuration
49
     *                                        file does not exist or is not
50
     *                                        readable.
51
     * @throws     <b>ParseException</b> If a requested configuration file is
52
     *                                   improperly formatted.
53
     *
54
     * @author     David Zülke <[email protected]>
55
     * @since      0.11.0
56
     */
57 View Code Duplication
    public function execute(XmlConfigDomDocument $document)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    {
59
        // set up our default namespace
60
        $document->setDefaultNamespace(self::XML_NAMESPACE, 'rbac_definitions');
61
        
62
        $data = array();
63
64
        foreach ($document->getConfigurationElements() as $cfg) {
65
            if (!$cfg->has('roles')) {
66
                continue;
67
            }
68
            
69
            $this->parseRoles($cfg->get('roles'), null, $data);
70
        }
71
72
        $code = "return " . var_export($data, true) . ";";
73
        
74
        return $this->generate($code, $document->documentURI);
75
    }
76
    
77
    /**
78
     * Parse a 'roles' node.
79
     *
80
     * @param      mixed  $roles  The "roles" node (element or node list)
81
     * @param      string $parent The name of the parent role, or null.
82
     * @param      array  $data   A reference to the output data array.
83
     *
84
     * @author     David Zülke <[email protected]>
85
     * @since      0.11.0
86
     */
87
    protected function parseRoles($roles, $parent, &$data)
88
    {
89
        /** @var XmlConfigDomElement $role */
90
        foreach ($roles as $role) {
91
            $name = $role->getAttribute('name');
92
            $entry = array();
93
            $entry['parent'] = $parent;
94
            $entry['permissions'] = array();
95
            if ($role->has('permissions')) {
96
                /** @var XmlConfigDomElement $permission */
97
                foreach ($role->get('permissions') as $permission) {
98
                    $entry['permissions'][] = $permission->getValue();
99
                }
100
            }
101
            if ($role->has('roles')) {
102
                $this->parseRoles($role->get('roles'), $name, $data);
103
            }
104
            $data[$name] = $entry;
105
        }
106
    }
107
}
108