Completed
Push — feature/add_relatives_from_ski... ( 1e8e28...795d93 )
by Tristan
16:16 queued 09:08
created

Controller::getRelativeIds()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 20
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
                foreach ($relatives[$relativeType] as $relativeInput) {
20
                    $relativeIds[] = $relativeInput['id'];
21
                }
22
            }
23
        }
24
        return $relativeIds;
25
    }
26
27
    public function shiftFirstLevelArrayKeysToBottom(array $nestedArray) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function shiftFirstLevelArrayKeysToBottom()
Loading history...
28
        $expandedArray = $this->expandNestedArraysIntoKeyListAndValue($nestedArray);
29
        $rotatedArray = $this->rotateKeys($expandedArray, 1);
30
        $mergedArray = $this->mergeExpandedTrees($rotatedArray);
31
        return $mergedArray;
32
    }
33
34
    protected function addKeyAsFinalIndex($finalKey, $array) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addKeyAsFinalIndex()
Loading history...
35
        if (!is_array($array)) {
36
            return [$finalKey => $array];
37
        } else {
38
            $newArray = [];
39
            foreach($array as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
40
                $newArray[$key] = $this->addKeyAsFinalIndex($finalKey, $value);
41
            }
42
            return $newArray;
43
        }
44
    }
45
46
    protected function expandNestedArraysIntoKeyListAndValue($nestedArray) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function expandNestedArraysIntoKeyListAndValue()
Loading history...
47
        if (!is_array($nestedArray)) {
48
            $expandedArray = [
49
                [
50
                    'keys' => [],
51
                    'value' => $nestedArray,
52
                ]
53
            ];
54
            return $expandedArray;
55
        } else {
56
            $expandedArray = [];
57
            foreach($nestedArray as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
58
                $subArray = $this->expandNestedArraysIntoKeyListAndValue($value);
59
                foreach($subArray as $item) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
60
                    array_unshift($item['keys'], $key);
61
                    $expandedArray[] = $item;
62
                }
63
            }
64
            return $expandedArray;
65
        }
66
    }
67
68
    protected function mergeExpandedTrees($expandedArray) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function mergeExpandedTrees()
Loading history...
69
        $mergedArray = [];
70
        foreach($expandedArray as $item) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
71
            $tail = &$mergedArray;
72
            $size = count($item['keys']);
73
            $i = 0;
74
            foreach($item['keys'] as $key) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
75
                $i = ($i + 1);
76
                //Check if this is the last key
77
                if ($i == ($size)) {
78
                    if (!isset($tail[$key])) {
79
                        $tail[$key] = $item['value'];
80
                    } else if (!is_array($tail[$key])) {
81
                        $value = $tail[$key];
82
                        $tail[$key] = [$value, $item['value']];
83
                    } else {
84
                        array_push($tail[$key], $item['value']);
85
                    }
86
                } else {
87
                    //If this is not the last key, it needs to contain an array
88
                    if (!isset($tail[$key])) {
89
                        $tail[$key] = [];
90
                    } else if (!is_array($tail[$key])) {
91
                        $value = $tail[$key];
92
                        $tail[$key] = [$value];
93
                    }
94
                    $tail = &$tail[$key];
95
                }
96
            }
97
        }
98
        return $mergedArray;
99
    }
100
101
    protected function rotateKeys($expandedArray, $steps) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function rotateKeys()
Loading history...
102
        $rotatedArray = [];
103
        foreach($expandedArray as $item) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
104
            for ($i=0; $i<$steps; $i++) {
105
                array_push($item['keys'], array_shift($item['keys']));
106
            }
107
            $rotatedArray[] = $item;
108
        }
109
        return $rotatedArray;
110
    }
111
112
    /**
113
     * Formats the $content array into JSON.
114
     *
115
     * @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...
116
     * @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...
117
     */
118
    protected function formatAjaxResponse(array $content)
119
    {
120
        return response()->json($content);
1 ignored issue
show
Bug Best Practice introduced by
The expression return response()->json($content) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
121
    }
122
}
123