Completed
Branch test (54f5ae)
by Elf
01:11
created

HashidManager::getConfigWithoutDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use InvalidArgumentException;
6
use Illuminate\Support\Manager;
7
8
class HashidManager extends Manager
9
{
10
    /**
11
     * Get a hashid connection instance.
12
     *
13
     * @param  string|null  $name
14
     * @return mixed
15
     */
16
    public function connection($name = null)
17
    {
18
        return $this->driver($name);
19
    }
20
21
    /**
22
     * Get all of the created connections.
23
     *
24
     * @return array
25
     */
26
    public function getConnections()
27
    {
28
        return $this->getDrivers();
29
    }
30
31
    /**
32
     * Get the default connection name.
33
     *
34
     * @return string
35
     */
36
    public function getDefaultConnection()
37
    {
38
        return $this->app['config']['hashid.default'];
39
    }
40
41
    /**
42
     * Set the default connection name.
43
     *
44
     * @param  string  $name
45
     * @return $this
46
     */
47
    public function setDefaultConnection($name)
48
    {
49
        $this->app['config']['hashid.default'] = $name;
50
51
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getDefaultDriver()
58
    {
59
        return $this->getDefaultConnection();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function createDriver($name)
66
    {
67
        $config = $this->app['config']->get("hashid.connections.{$name}", []);
68
69
        if (isset($this->customCreators[$name])) {
70
            return $this->customCreators[$name]($this->app, $config, $name);
71
        }
72
73
        if (isset($config['driver']) &&
74
            isset($this->customCreators[$driver = $config['driver']])
75
        ) {
76
            return $this->customCreators[$driver]($this->app, $this->getConfigWithoutDriver($config), $name);
77
        }
78
79
        return $this->createConnection($config);
80
    }
81
82
    /**
83
     * Create a new hashid connection instance.
84
     *
85
     * @param  array  $config
86
     * @return mixed
87
     *
88
     * @throws \InvalidArgumentException
89
     */
90
    protected function createConnection(array $config)
91
    {
92
        if (! isset($config['driver'])) {
93
            throw new InvalidArgumentException('A driver must be specified.');
94
        }
95
96
        $driver = $config['driver'];
97
98
        if ($this->app->bound($key = "hashid.connection.{$driver}")) {
99
            if ($this->app->isShared($key)) {
100
                return $this->app->make($key);
101
            }
102
103
            $makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make';
104
105
            return $this->app->{$makeWith}($key, [
106
                'app' => $this->app,
107
                'config' => $this->getConfigWithoutDriver($config),
108
            ]);
109
        }
110
111
        throw new InvalidArgumentException("Unsupported driver [$driver]");
112
    }
113
114
    /**
115
     * Get connection configuration without "driver" element.
116
     *
117
     * @param  array  $config
118
     * @return array
119
     */
120
    protected function getConfigWithoutDriver($config)
121
    {
122
        unset($config['driver']);
123
124
        return $config;
125
    }
126
}
127