Completed
Push — master ( abfdab...df31dd )
by Andreas
08:27
created

midcom_helper_filesync_exporter_structure   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
B read_node() 0 34 3
A read_structure() 0 12 1
A export() 0 10 1
C draw_array() 0 43 8
1
<?php
2
/**
3
 * @package midcom.helper.filesync
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * Export a site structure to the format used by Site Wizard
11
 *
12
 * @package midcom.helper.filesync
13
 */
14
class midcom_helper_filesync_exporter_structure extends midcom_helper_filesync_exporter
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
{
16
    private function read_node(midcom_db_topic $node)
17
    {
18
        $node_array = array();
19
        $node_array['name'] = $node->name;
20
        $node_array['title'] = $node->extra;
21
        $node_array['component'] = $node->component;
22
        $node_array['style'] = $node->style;
23
        $node_array['style_inherit'] = $node->styleInherit;
24
25
        // Per-component specialties
26
        switch ($node->component)
27
        {
28
            case 'net.nehmer.static':
29
                $node_array['create_index'] = true;
30
        }
31
32
        // Get parameters
33
        $node_array['parameters'] = $node->list_parameters();
34
35
        // TODO: Implement ACL exporting
36
        $node_array['acl'] = array();
37
38
        // Recurse subnodes
39
        $node_array['nodes'] = array();
40
        $qb = midcom_db_topic::new_query_builder();
41
        $qb->add_constraint('up', '=', $node->id);
42
        $children = $qb->execute();
43
        foreach ($children as $child)
0 ignored issues
show
Bug introduced by
The expression $children of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
44
        {
45
            $node_array['nodes'][$child->name] = $this->read_node($child);
46
        }
47
48
        return $node_array;
49
    }
50
51
    public function read_structure(midcom_db_topic $root_node, $structure_name)
52
    {
53
        // Prepare structure
54
        $structure = array();
55
        $structure[$structure_name] = array();
56
        $structure[$structure_name]['name'] = $structure_name;
57
        $structure[$structure_name]['title'] = midcom::get()->config->get('midcom_site_title');
58
        // Read the topic data
59
        $structure[$structure_name]['root'] = $this->read_node($root_node);
60
61
        file_put_contents("{$this->root_dir}{$structure_name}.inc", $this->draw_array($structure));
62
    }
63
64
    private function draw_array($array, $prefix = '')
65
    {
66
        $data = '';
67
        foreach ($array as $key => $val)
68
        {
69
            $data .= $prefix;
70
            if (!is_numeric($key))
71
            {
72
                $data .= "'{$key}' => ";
73
            }
74
75
            switch (gettype($val))
76
            {
77
                case 'boolean':
78
                    $data .= ($val)?'true':'false';
79
                    break;
80
                case 'array':
81
                    if (empty($val))
82
                    {
83
                        $data .= 'array()';
84
                    }
85
                    else
86
                    {
87
                        $data .= "array\n{$prefix}(\n" . $this->draw_array($val, "{$prefix}    ") . "{$prefix})";
88
                    }
89
                    break;
90
91
                default:
92
                    if (is_numeric($val))
93
                    {
94
                        $data .= $val;
95
                    }
96
                    else
97
                    {
98
                        $data .= "'" . str_replace("'", "\'", $val) . "'";
99
                    }
100
            }
101
102
            $data .= ",\n";
103
104
        }
105
        return $data;
106
    }
107
108
    public function export()
109
    {
110
        // Generate a safe name for the structure
111
        $generator = midcom::get()->serviceloader->load('midcom_core_service_urlgenerator');
112
        $structure_name = $generator->from_string(midcom::get()->get_page_prefix());
113
114
        $root_topic = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC);
115
116
        $this->read_structure($root_topic, $structure_name);
117
    }
118
}
119