Completed
Push — master ( 348067...ae37c1 )
by Zach
03:29 queued 01:39
created

Config::getDefault()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Config;
4
5
class Config
6
{
7
    /**
8
     * Instance of self.
9
     *
10
     * @var Config
11
     */
12
    private static $instance;
13
14
    /**
15
     * Yarak config array.
16
     *
17
     * @var array
18
     */
19
    protected $configArray;
20
21
    /**
22
     * Default setting values.
23
     *
24
     * @var array
25
     */
26
    const DEFAULTS = [
27
        'migratorType'        => 'fileDate',
28
        'migrationRepository' => 'database',
29
    ];
30
31
    /**
32
     * Private constructor.
33
     */
34
    private function __construct(array $configArray)
35
    {
36
        $this->configArray = $configArray;
37
    }
38
39
    /**
40
     * Get instance of self with config array set.
41
     *
42
     * @param array $configArray
43
     *
44
     * @return Config
45
     */
46
    public static function getInstance(array $configArray = [])
47
    {
48
        if (empty(self::$instance)) {
49
            self::$instance = new self($configArray);
50
        }
51
52
        return self::$instance;
53
    }
54
55
    /**
56
     * Get a value from the config array.
57
     *
58
     * @param string|array $value
59
     *
60
     * @return mixed
61
     */
62
    public function get($value)
63
    {
64
        if (!is_array($value)) {
65
            $value = [$value];
66
        }
67
68
        $current = $this->configArray;
69
70
        foreach ($value as $configItem) {
71
            if (!isset($current[$configItem])) {
72
                return $this->getDefault($configItem);
73
            } else {
74
                $current = $current[$configItem];
75
            }
76
        }
77
78
        return $current;
79
    }
80
81
    /**
82
     * Get a setting's default value.
83
     *
84
     * @param string $value
85
     *
86
     * @return mixed|null
87
     */
88
    public function getDefault($value)
89
    {
90
        if (array_key_exists($value, self::DEFAULTS)) {
91
            return self::DEFAULTS[$value];
92
        }
93
    }
94
95
    /**
96
     * Return config array.
97
     *
98
     * @return array
99
     */
100
    public function getAll()
101
    {
102
        return $this->configArray;
103
    }
104
105
    /**
106
     * Return the database directory path.
107
     *
108
     * @return string
109
     */
110
    public function getDatabaseDirectory()
111
    {
112
        return $this->get(['application', 'databaseDir']);
113
    }
114
115
    /**
116
     * Return the migration directory path.
117
     *
118
     * @param string $path
119
     *
120
     * @return string
121
     */
122
    public function getMigrationDirectory($path = '')
123
    {
124
        return $this->getDatabaseDirectory().'migrations/'.$path;
125
    }
126
127
    /**
128
     * Return the factory directory path.
129
     *
130
     * @param string $path
131
     *
132
     * @return string
133
     */
134
    public function getFactoryDirectory($path = '')
135
    {
136
        return $this->getDatabaseDirectory().'factories/'.$path;
137
    }
138
139
    /**
140
     * Return the seeds directory path.
141
     *
142
     * @param string $path
143
     *
144
     * @return string
145
     */
146
    public function getSeedDirectory($path = '')
147
    {
148
        return $this->getDatabaseDirectory().'seeds/'.$path;
149
    }
150
151
    /**
152
     * Make database directory structure if it doesn't exist.
153
     */
154
    public function getAllDatabaseDirectories()
155
    {
156
        return [
157
            $this->getDatabaseDirectory(),
158
            $this->getMigrationDirectory(),
159
            $this->getFactoryDirectory(),
160
            $this->getSeedDirectory(),
161
        ];
162
    }
163
}
164