ConfigHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generatorConfig() 0 14 3
A isAssociativeArray() 0 3 2
A mergeConfig() 0 18 5
1
<?php
2
3
namespace CSlant\LaraGenAdv\Helpers;
4
5
use CSlant\LaraGenAdv\Exceptions\LaravelGeneratorAdvancedException;
6
7
class ConfigHelper
8
{
9
    /**
10
     * Get config.
11
     *
12
     * @param  string|null  $generatorName
13
     *
14
     * @return array
15
     *
16
     * @throws LaravelGeneratorAdvancedException
17
     */
18
    public function generatorConfig(?string $generatorName = null): array
19
    {
20
        if ($generatorName === null) {
21
            $generatorName = config('lara-gen-adv.default');
22
        }
23
24
        $defaults = config('lara-gen-adv.defaults', []);
25
        $generators = config('lara-gen-adv.generators', []);
26
27
        if (!isset($generators[$generatorName])) {
28
            throw new LaravelGeneratorAdvancedException('Generator name not found');
29
        }
30
31
        return $this->mergeConfig($defaults, $generators[$generatorName]);
32
    }
33
34
    /**
35
     * Merge config.
36
     *
37
     * @param  array  $defaults
38
     * @param  array  $generatorName
39
     * @return array
40
     */
41
    private function mergeConfig(array $defaults, array $generatorName): array
42
    {
43
        $merged = $defaults;
44
45
        foreach ($generatorName as $key => &$value) {
46
            if (isset($defaults[$key])
47
                && $this->isAssociativeArray($defaults[$key])
48
                && $this->isAssociativeArray($value)
49
            ) {
50
                $merged[$key] = $this->mergeConfig($defaults[$key], $value);
51
52
                continue;
53
            }
54
55
            $merged[$key] = $value;
56
        }
57
58
        return $merged;
59
    }
60
61
    /**
62
     * Check is associative key array.
63
     *
64
     * @param  mixed  $key
65
     * @return bool
66
     */
67
    private function isAssociativeArray(mixed $key): bool
68
    {
69
        return is_array($key) && count(array_filter(array_keys($key), 'is_string')) > 0;
70
    }
71
}
72