CorsPolicyManager::createDriver()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
1
<?php
2
3
namespace ShiftOneLabs\LaravelCors;
4
5
use LogicException;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Manager;
8
9
class CorsPolicyManager extends Manager
10
{
11
    /** @var array $config */
12
    protected $config;
13
14
    /**
15
     * Create a new manager instance.
16
     *
17
     * @param  \Illuminate\Foundation\Application  $app
18
     * @param  array  $config
19
     *
20
     * @return void
21
     */
22 230
    public function __construct($app, $config)
23
    {
24 230
        parent::__construct($app);
25
26 230
        $this->config = $config;
27 230
    }
28
29
    /**
30
     * Get the default driver name.
31
     *
32
     * @return string
33
     */
34 92
    public function getDefaultDriver()
35
    {
36 92
        return $this->config('default');
37
    }
38
39
    /**
40
     * Create a new Cors policy instance.
41
     *
42
     * @param  string  $profile
43
     *
44
     * @return \ShiftOneLabs\LaravelCors\CorsPolicy
45
     */
46 184
    public function make($profile = null)
47
    {
48 184
        return $this->driver($profile);
49
    }
50
51
    /**
52
     * Create a new driver instance.
53
     *
54
     * @param  string  $profile
55
     *
56
     * @return \ShiftOneLabs\LaravelCors\CorsPolicy
57
     *
58
     * @throws \LogicException
59
     */
60 184
    protected function createDriver($profile)
61
    {
62 184
        if (isset($this->customCreators[$profile])) {
63
            return $this->callCustomCreator($profile);
64
        }
65
66 184
        $config = $this->config('profiles.'.$profile);
67
68 184
        if (empty($config)) {
69 46
            throw new LogicException('CORS profile ['.$profile.'] not found.');
70
        }
71
72 138
        return new CorsPolicy($config);
73
    }
74
75
    /**
76
     * Get a value from the config array.
77
     *
78
     * @param  string  $key
79
     * @param  mixed|null  $default
80
     *
81
     * @return mixed
82
     */
83 184
    protected function config($key = null, $default = null)
84
    {
85 184
        if (empty($key)) {
86
            return $this->config;
87
        }
88
89 184
        return Arr::get($this->config, $key, $default);
90
    }
91
}
92