Passed
Push — feature/screening-plan ( d3f64c...59054f )
by Tristan
08:31 queued 02:39
created

Controller::formatAjaxResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Http\Controllers;
4
5
use Illuminate\Foundation\Bus\DispatchesJobs;
6
use Illuminate\Routing\Controller as BaseController;
7
use Illuminate\Foundation\Validation\ValidatesRequests;
8
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
9
10
class Controller extends BaseController
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Controller
Loading history...
11
{
12
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
13
14
    public function getRelativeIds($input, $relativeType) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRelativeIds()
Loading history...
15
        $relativeIds = [];
16
        if (isset($input['relatives'])) {
17
            $relatives = $input['relatives'];
18
            if (isset($relatives[$relativeType])) {
19
                if (isset($relatives[$relativeType]['new'])) {
20
                    foreach($relatives[$relativeType]['new'] as $relativeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
21
                        $relativeIds[] = $relativeInput['id'];
22
                    }
23
                }
24
                if (isset($relatives[$relativeType]['old'])) {
25
                    foreach($relatives[$relativeType]['old'] as $relativeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
26
                        $relativeIds[] = $relativeInput['id'];
27
                    }
28
                }
29
            }
30
        }
31
        return $relativeIds;
32
    }
33
34
    public function shiftFirstLevelArrayKeysToBottom(array $nestedArray) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function shiftFirstLevelArrayKeysToBottom()
Loading history...
35
        $expandedArray = $this->expandNestedArraysIntoKeyListAndValue($nestedArray);
36
        $rotatedArray = $this->rotateKeys($expandedArray, 1);
37
        $mergedArray = $this->mergeExpandedTrees($rotatedArray);
38
        return $mergedArray;
39
    }
40
41
    protected function addKeyAsFinalIndex($finalKey, $array) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addKeyAsFinalIndex()
Loading history...
42
        if (!is_array($array)) {
43
            return [$finalKey => $array];
44
        } else {
45
            $newArray = [];
46
            foreach($array as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
47
                $newArray[$key] = $this->addKeyAsFinalIndex($finalKey, $value);
48
            }
49
            return $newArray;
50
        }
51
    }
52
53
    protected function expandNestedArraysIntoKeyListAndValue($nestedArray) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function expandNestedArraysIntoKeyListAndValue()
Loading history...
54
        if (!is_array($nestedArray)) {
55
            $expandedArray = [
56
                [
57
                    'keys' => [],
58
                    'value' => $nestedArray,
59
                ]
60
            ];
61
            return $expandedArray;
62
        } else {
63
            $expandedArray = [];
64
            foreach($nestedArray as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
65
                $subArray = $this->expandNestedArraysIntoKeyListAndValue($value);
66
                foreach($subArray as $item) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
67
                    array_unshift($item['keys'], $key);
68
                    $expandedArray[] = $item;
69
                }
70
            }
71
            return $expandedArray;
72
        }
73
    }
74
75
    protected function mergeExpandedTrees($expandedArray) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function mergeExpandedTrees()
Loading history...
76
        $mergedArray = [];
77
        foreach($expandedArray as $item) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
78
            $tail = &$mergedArray;
79
            $size = count($item['keys']);
80
            $i = 0;
81
            foreach($item['keys'] as $key) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
82
                $i = ($i + 1);
83
                //Check if this is the last key
84
                if ($i == ($size)) {
85
                    if (!isset($tail[$key])) {
86
                        $tail[$key] = $item['value'];
87
                    } else if (!is_array($tail[$key])) {
88
                        $value = $tail[$key];
89
                        $tail[$key] = [$value, $item['value']];
90
                    } else {
91
                        array_push($tail[$key], $item['value']);
92
                    }
93
                } else {
94
                    //If this is not the last key, it needs to contain an array
95
                    if (!isset($tail[$key])) {
96
                        $tail[$key] = [];
97
                    } else if (!is_array($tail[$key])) {
98
                        $value = $tail[$key];
99
                        $tail[$key] = [$value];
100
                    }
101
                    $tail = &$tail[$key];
102
                }
103
            }
104
        }
105
        return $mergedArray;
106
    }
107
108
    protected function rotateKeys($expandedArray, $steps) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function rotateKeys()
Loading history...
109
        $rotatedArray = [];
110
        foreach($expandedArray as $item) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
111
            for ($i=0; $i<$steps; $i++) {
112
                array_push($item['keys'], array_shift($item['keys']));
113
            }
114
            $rotatedArray[] = $item;
115
        }
116
        return $rotatedArray;
117
    }
118
119
    /**
120
     * Formats the $content array into JSON.
121
     *
122
     * @param string[] $content The array being returned in response.
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
123
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
124
     */
125
    protected function formatAjaxResponse(array $content): \Illuminate\Http\Response
126
    {
127
        return response()->json($content);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($content) returns the type Illuminate\Http\JsonResponse which is incompatible with the type-hinted return Illuminate\Http\Response.
Loading history...
128
    }
129
}
130