Completed
Push — master ( 15c9e2...d1079a )
by wen
03:11
created

Factory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

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 Factory
9
{
10
    protected $configs = [];
11
12
    public function __construct()
13
    {
14
15
    }
16
17
    public function makeFromUri($uri)
18
    {
19
        $name = $this->getNestedConfigName($uri);
20
        return $this->make($name);
21
    }
22
23
    protected function getNestedConfigName($uri)
24
    {
25
        return str_replace('/', '.', $uri);
26
    }
27
28
    public function make($name)
29
    {
30
        if (isset($this->configs[$name])) {
31
            return $this->configs[$name];
32
        }
33
        $options = $this->getConfigOptions($name);
34
35
        return $this->configs[$name] = $options ? new ModelConfig($options) : null;
36
    }
37
38
    protected function getConfigOptions($name)
39
    {
40
        if (file_exists($this->getConfigFilePath($name))) {
41
            return config(config('admin.model_config_dir') . '.' . $name);
42
        }
43
        throw new NotFoundHttpException("not found model({$name}) config file");
44
    }
45
46
    protected function getConfigFilePath($name)
47
    {
48
        $modelPath = config_path(config('admin.model_config_dir'));
49
        if (!is_dir($modelPath)) {
50
            throw new InvalidArgumentException('admin config(model_config_dir) is not a directory');
51
        }
52
        $file = str_replace('.', DIRECTORY_SEPARATOR, $name) . '.php';
53
        return $modelPath . DIRECTORY_SEPARATOR . $file;
54
    }
55
}
56