Completed
Push — develop ( 4bf430...c74260 )
by
unknown
18:30
created

DefaultCategoriesBuilder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 83
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A build() 0 12 2
A loadCategoriesConfig() 0 16 4
B buildCategory() 0 21 7
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Jobs\Repository;
12
13
use Jobs\Entity\Category;
14
15
/**
16
 * ${CARET}
17
 * 
18
 * @author Mathias Gelhausen <[email protected]>
19
 * @todo write test 
20
 */
21
class DefaultCategoriesBuilder 
22
{
23
24
    /**
25
     * The module config path.
26
     *
27
     * @var string
28
     */
29
    private $moduleConfigPath;
30
31
    /**
32
     * The global config paths.
33
     *
34
     * @var string[]
35
     */
36
    private $globalConfigPaths;
37
38
    /**
39
     * Prototype of a Category
40
     *
41
     * @var Category
42
     */
43
    private $categoryPrototype;
44
45
    public function __construct($moduleConfigPath, array $globalConfigPaths, Category $categoryPrototype)
46
    {
47
        $this->moduleConfigPath = $moduleConfigPath;
48
        $this->globalConfigPaths = $globalConfigPaths;
49
        $this->categoryPrototype = $categoryPrototype;
50
    }
51
    
52
    public function build($type)
53
    {
54
        $categories = $this->loadCategoriesConfig($type);
55
        
56
        if (!is_array($categories)) {
57
            $categories = ['name' => $type, 'value' => $type];
58
        }
59
        
60
        $category = $this->buildCategory($categories);
61
62
        return $category;
63
    }
64
    
65
    private function loadCategoriesConfig($type)
66
    {
67
        $file = "jobs.categories.$type.php";
68
69
        foreach ($this->globalConfigPaths as $path) {
70
            if (file_exists($path . $file)) {
71
                return include $path . $file;
72
            }
73
        }
74
75
        if (file_exists($this->moduleConfigPath . $file)) {
76
            return include $this->moduleConfigPath . $file;
77
        }
78
79
        return false;
80
    }
81
82
    private function buildCategory($spec)
83
    {
84
        if (is_string($spec)) {
85
            $spec = ['name' => $spec];
86
        }
87
88
        $category = clone $this->categoryPrototype;
89
        $category
90
            ->setName(isset($spec['name']) ? $spec['name'] : '')
91
            ->setValue(isset($spec['value']) ? $spec['value'] : '')
92
        ;
93
94
        if (isset($spec['children']) && is_array($spec['children'])) {
95
            foreach ($spec['children'] as $childSpec) {
96
                $child = $this->buildCategory($childSpec);
97
                $category->addChild($child);
98
            }
99
        }
100
101
        return $category;
102
    }
103
}