Completed
Pull Request — master (#39)
by Dominik
02:00
created

Render::validateConstructionPlan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt;
4
5
class Render
6
{
7
    public static function fromConstructionPlan($constructionPlan)
8
    {
9
        if (false === self::validateConstructionPlan($constructionPlan)) {
10
            return '';
11
        }
12
13
        $areaHtml = self::extractAreaHtml($constructionPlan);
14
15
        return self::applyRenderFilters($constructionPlan, $areaHtml);
16
    }
17
18
    protected static function validateConstructionPlan($constructionPlan)
19
    {
20
        $valid = true;
0 ignored issues
show
Unused Code introduced by
$valid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
21
        $valid = self::validateConstructionPlanIsNonEmptyArray($constructionPlan);
0 ignored issues
show
Unused Code introduced by
$valid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
        $valid = self::validateConstructionPlanName($constructionPlan);
0 ignored issues
show
Unused Code introduced by
$valid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23
        $valid = self::validateConstructionPlanData($constructionPlan);
24
        return $valid;
25
    }
26
27
    protected static function validateConstructionPlanIsNonEmptyArray($constructionPlan)
28
    {
29
        if (!is_array($constructionPlan)) {
30
            trigger_error(
31
                'Construction Plan is not an array! ' . ucfirst(gettype($constructionPlan)) . ' given.',
32
                E_USER_WARNING
33
            );
34
            return false;
35
        } elseif (empty($constructionPlan)) {
36
            trigger_error('Empty Construction Plan array!', E_USER_WARNING);
37
            return false;
38
        }
39
    }
40
41
    protected static function validateConstructionPlanName($constructionPlan)
42
    {
43
        if (!isset($constructionPlan['name'])) {
44
            trigger_error('Construction Plan is missing key: "name"', E_USER_WARNING);
45
            return false;
46
        } elseif (!is_string($constructionPlan['name'])) {
47
            trigger_error('Construction Plan key "name" is not a string!', E_USER_WARNING);
48
            return false;
49
        }
50
    }
51
52
    protected static function validateConstructionPlanData($constructionPlan)
53
    {
54
        if (!isset($constructionPlan['data'])) {
55
            trigger_error('Construction Plan is missing key: "data"', E_USER_WARNING);
56
            return false;
57
        } elseif (!is_array($constructionPlan['data'])) {
58
            trigger_error('Construction Plan key "data" is not an array!', E_USER_WARNING);
59
            return false;
60
        }
61
    }
62
63
    protected static function extractAreaHtml($constructionPlan)
64
    {
65
        $areaHtml = [];
66
        if (array_key_exists('areas', $constructionPlan)) {
67
            if (!is_array($constructionPlan['areas'])) {
68
                trigger_error('Construction Plan key "areas" is not an array!', E_USER_WARNING);
69
            } else {
70
                $areas = $constructionPlan['areas'];
71
                $areaHtml = array_reduce(
72
                    array_keys($areas),
73
                    function ($carry, $areaName) use ($areas) {
74
                        $carry[$areaName] = self::joinAreaComponents($areaName, $areas);
75
                        return $carry;
76
                    },
77
                    []
78
                );
79
            }
80
        }
81
        return $areaHtml;
82
    }
83
84
    protected static function joinAreaComponents($areaName, $areas)
85
    {
86
        $components = $areas[$areaName];
87
88
        // "areas" need to be an associative array
89
        if (is_int($areaName)) {
90
            trigger_error('Area name is not defined!', E_USER_WARNING);
91
            return '';
92
        }
93
        if (!is_array($components)) {
94
            trigger_error("Area \"{$areaName}\" is not an array!", E_USER_WARNING);
95
            return '';
96
        }
97
        return implode('', array_map('self::fromConstructionPlan', $components));
98
    }
99
100
    protected static function applyRenderFilters($constructionPlan, $areaHtml)
101
    {
102
        $componentData = $constructionPlan['data'];
103
        $componentName = $constructionPlan['name'];
104
105
        $output = apply_filters('Flynt/renderComponent', null, $componentName, $componentData, $areaHtml);
106
        $output = apply_filters(
107
            "Flynt/renderComponent?name={$componentName}",
108
            $output,
109
            $componentName,
110
            $componentData,
111
            $areaHtml
112
        );
113
114
        return is_null($output) ? '' : $output;
115
    }
116
}
117