Completed
Push — dev ( 348067...b33ea3 )
by Zach
02:21
created

Config::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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
72
            if (!isset($current[$configItem])) {
73
                return $this->getDefault($configItem);
74
            } else {
75
                $current = $current[$configItem];
76
            }
77
        }
78
79
        return $current;
80
    }
81
82
    /**
83
     * Get a setting's default value.
84
     *
85
     * @param string $value
86
     *
87
     * @return mixed|null
88
     */
89
    public function getDefault($value)
90
    {
91
        if (isset(self::DEFAULTS[$value])) {
92
            return self::DEFAULTS[$value];
93
        }
94
95
        return;
96
    }
97
98
    /**
99
     * Return config array.
100
     *
101
     * @return array
102
     */
103
    public function getAll()
104
    {
105
        return $this->configArray;
106
    }
107
108
    /**
109
     * Return the database directory path.
110
     *
111
     * @return string
112
     */
113
    public function getDatabaseDirectory()
114
    {
115
        return $this->get(['application', 'databaseDir']);
116
    }
117
118
    /**
119
     * Return the migration directory path.
120
     *
121
     * @param string $path
122
     *
123
     * @return string
124
     */
125
    public function getMigrationDirectory($path = '')
126
    {
127
        return $this->getDatabaseDirectory().'migrations/'.$path;
128
    }
129
130
    /**
131
     * Return the factory directory path.
132
     *
133
     * @param string $path
134
     *
135
     * @return string
136
     */
137
    public function getFactoryDirectory($path = '')
138
    {
139
        return $this->getDatabaseDirectory().'factories/'.$path;
140
    }
141
142
    /**
143
     * Return the seeds directory path.
144
     *
145
     * @param string $path
146
     *
147
     * @return string
148
     */
149
    public function getSeedDirectory($path = '')
150
    {
151
        return $this->getDatabaseDirectory().'seeds/'.$path;
152
    }
153
154
    /**
155
     * Make database directory structure if it doesn't exist.
156
     */
157
    public function getAllDatabaseDirectories()
158
    {
159
        return [
160
            $this->getDatabaseDirectory(),
161
            $this->getMigrationDirectory(),
162
            $this->getFactoryDirectory(),
163
            $this->getSeedDirectory(),
164
        ];
165
    }
166
}
167