Completed
Push — master ( dde429...22637e )
by Andrii
20:00
created

Config::getItem()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\components;
13
14
use hidev\helpers\Helper;
15
use Yii;
16
use yii\base\BootstrapInterface;
17
use yii\base\InvalidParamException;
18
use yii\helpers\ArrayHelper;
19
20
/**
21
 * The Config. Keeps config and Goals.
22
 */
23
class Config extends \hiqdev\yii2\collection\Object
24
{
25
    public $file = '.hidev/config.yml';
26
27
    public function hasGoal($name)
28
    {
29
        return $this->hasItem($name);
30
    }
31
32
    /*public function findGoal($name)
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
    {
34
        $config = $this->getGoals()->get($name);
35
        return is_scalar($config) ? ['class' => $config] : (array) $config;
36
    }*/
37
38
    public function getItemConfig($name = null, array $config = [])
39
    {
40
        return ArrayHelper::merge([
41
            'goalName' => $name,
42
            'class'    => 'hidev\goals\DefaultGoal',
43
        ], $config);
44
    }
45
46
    protected function createItem($name, $config = [])
47
    {
48
        $config = is_scalar($config) ? ['class' => $config] : (array) $config;
49
        return Yii::createObject($this->getItemConfig($name, $config), [$name, Yii::$app]);
50
    }
51
52
    public function getItem($name)
53
    {
54
        if ($name === 'default') {
55
            return $this;
56
        }
57
        $item = &$this->_items[$name];
58
        #var_dump($item);
59
        #if (is_array($item) || is_null($item)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
        if (!is_object($item)) {
61
            $item = $this->createItem($name, $item ?: []);
62
        }
63
64
        return $item;
65
    }
66
67
    /**
68
     * Loads all the configs. Reads or creates if doesn't exist.
69
     * @void
70
     */
71
    public function loadAllConfigs()
72
    {
73
        if (!file_exists($this->file)) {
74
            throw new InvalidParamException('No config found. Use hidev init vendor/package');
75
        }
76
        if (Yii::$app->get('configs')) {
77
            foreach (Yii::$app->get as $path) {
78
                $this->includeConfig($path);
79
            }
80
        }
81
        $this->includeConfig($this->file);
82
        if ($this->has('include')) {
83
            foreach (Helper::csplit($this->rawItem('include')) as $path) {
84
                $this->includeConfig($path);
85
            }
86
        }
87
    }
88
89
    public function includeConfig($path)
90
    {
91
        $file = Yii::createObject(array_merge([
92
            'class' => 'hidev\base\File',
93
        ], is_array($path) ? $path : compact('path')));
94
        $this->setItems($file->load());
95
    }
96
}
97