Completed
Push — master ( 0f88da...dbea24 )
by wen
03:26
created

ConfigManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 53
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A makeFromUri() 0 5 1
A getNestedConfigName() 0 4 1
A make() 0 9 3
A getConfigOptions() 0 7 2
A getConfigFilePath() 0 9 2
1
<?php
2
3
namespace Sco\Admin\Config;
4
5
use Sco\Admin\Exceptions\InvalidArgumentException;
6
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
7
8
class ConfigManager
9
{
10
    /**
11
     * @var \Illuminate\Foundation\Application
12
     */
13
    protected $app;
14
15
    protected $configs = [];
16
17
    public function __construct($app)
18
    {
19
        $this->app = $app;
20
    }
21
22
    public function makeFromUri($uri)
23
    {
24
        $name = $this->getNestedConfigName($uri);
25
        return $this->make($name);
26
    }
27
28
    protected function getNestedConfigName($uri)
29
    {
30
        return str_replace('/', '.', $uri);
31
    }
32
33
    public function make($name)
34
    {
35
        if (isset($this->configs[$name])) {
36
            return $this->configs[$name];
37
        }
38
        $options = $this->getConfigOptions($name);
39
40
        return $this->configs[$name] = $options ? new ModelConfig($options) : null;
41
    }
42
43
    protected function getConfigOptions($name)
44
    {
45
        if (file_exists($this->getConfigFilePath($name))) {
46
            return config(config('admin.model_config_dir') . '.' . $name);
47
        }
48
        throw new NotFoundHttpException("not found model({$name}) config file");
49
    }
50
51
    protected function getConfigFilePath($name)
52
    {
53
        $modelPath = config_path(config('admin.model_config_dir'));
54
        if (!is_dir($modelPath)) {
55
            throw new InvalidArgumentException('admin config(model_config_dir) is not a directory');
56
        }
57
        $file = str_replace('.', DIRECTORY_SEPARATOR, $name) . '.php';
58
        return $modelPath . DIRECTORY_SEPARATOR . $file;
59
    }
60
}
61