Completed
Push — master ( b4eff9...d2ff01 )
by
unknown
01:32
created

Chart   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 133
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addSubset() 0 9 2
A render() 0 22 3
A addSimpleSubset() 0 8 1
A addCascadeSubset() 0 12 1
A callFunc() 0 4 1
A makeSubsetClass() 0 6 1
A dotNotationToArray() 0 15 2
1
<?php
2
3
namespace EnergieProduction\Chart;
4
5
use Closure;
6
use EnergieProduction\Subsets;
7
use Illuminate\Support\Collection;
8
9
Class Chart {
10
11
	protected $subsetList;
12
13
	/**
14
	 * [__construct description]
15
	 * @param \Illuminate\Support\Collection $subsetList
16
	 */
17
	public function __construct(Collection $subsetList)
18
	{
19
		$this->subsetList = $subsetList;
20
	}
21
22
	/**
23
	 * [addSubset description]
24
	 * @param string  $subset
25
	 * @param \Closure $closure
26
	 */
27
	public function addSubset($subset, Closure $closure)
28
	{
29
		if (str_contains($subset, '.')) {
30
			$this->addCascadeSubset($subset, $closure);
31
		}
32
		else {
33
			$this->addSimpleSubset($subset, $closure);
34
		}
35
	}
36
37
	/**
38
	 * [render description]
39
	 * @return json [description]
40
	 */
41
	public function render()
42
	{
43
		$formatedChart = [];
44
45
		foreach ($this->subsetList as $subset) {
46
47
			$render = $subset->render();
48
49
			if ($subset->cascade) {
50
51
				$tmpArray = [];
52
53
				$this->dotNotationToArray($tmpArray, $subset->cascade, $render);
54
55
				$render = $tmpArray;
56
			}
57
58
			$formatedChart = array_merge_recursive($formatedChart, $render);
59
		}
60
61
		return json_encode($formatedChart);
62
	}
63
64
	/**
65
	 * [addSimpleSubset description]
66
	 * @param string  $subset
67
	 * @param \Closure $closure
68
	 */
69
	protected function addSimpleSubset($subset, Closure $closure)
70
	{
71
		$subset = $this->makeSubsetClass($subset);
72
73
		$this->callFunc($closure, $subset);
74
75
		$this->subsetList->push($subset);
76
	}
77
78
	/**
79
	 * [addCascadeSubset description]
80
	 * @param string  $cascadeSubsetNotation
81
	 * @param \Closure $closure
82
	 */
83
	protected function addCascadeSubset($cascadeSubsetNotation, Closure $closure)
84
	{
85
		$subsetsList = explode('.', $cascadeSubsetNotation);
86
87
		$subset = $this->makeSubsetClass(end($subsetsList));
88
89
		$subset->setCascade($cascadeSubsetNotation);
90
91
		$this->callFunc($closure, $subset);
92
93
		$this->subsetList->push($subset);
94
	}
95
96
	/**
97
	 * [callFunc description]
98
	 * @param  \Closure $closure
99
	 * @param  [type] $class
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
100
	 * @return [type
0 ignored issues
show
Documentation introduced by
The doc-type type">[type could not be parsed: Unknown type name "[" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
101
	 */
102
	protected function callFunc(Closure $closure, $class)
103
	{
104
		return call_user_func($closure, $class);
105
	}
106
107
	/**
108
	 * [makeSubsetClass description]
109
	 * @param  [type] $subset
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
110
	 * @return [type]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
111
	 */
112
	protected function makeSubsetClass($subset)
113
	{
114
		$class = "Service\\Subsets\\" . ucfirst($subset);
115
116
		return new $class;
117
	}
118
119
	/**
120
	 * [dotNotationToArray description]
121
	 * @param  array  &$arr
122
	 * @param  string $path
123
	 * @param  array $val
124
	 * @return array
125
	 */
126
	protected function dotNotationToArray(array &$arr, $path, $val)
127
	{
128
	   $loc = &$arr;
129
130
	   $path = explode('.', $path);
131
132
	   array_pop($path);
133
134
	   foreach($path as $step)
135
	   {
136
	     $loc = &$loc[$step];
137
	   }
138
139
	   return $loc = $val;
140
	}
141
}
142