Completed
Push — master ( 1dadef...242226 )
by Elf
03:09
created

HashidManager::createDriver()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 3
c 4
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Manager;
7
use InvalidArgumentException;
8
9
class HashidManager extends Manager
10
{
11
    /**
12
     * The container instance.
13
     *
14
     * The $app property was removed in Laravel 8.x.
15
     *
16
     * @var \Illuminate\Contracts\Container\Container
17
     */
18
    protected $app;
19
20
    /**
21
     * Create a new manager instance.
22
     *
23
     * @param  \Illuminate\Contracts\Container\Container  $container
24
     * @return void
25
     */
26 56
    public function __construct($container)
27
    {
28 56
        parent::__construct($container);
29
30 56
        $this->app = $container;
31 56
    }
32
33
    /**
34
     * Get a hashid connection instance.
35
     *
36
     * @param  string|null  $name
37
     * @return mixed
38
     */
39 44
    public function connection($name = null)
40
    {
41 44
        return $this->driver($name);
42
    }
43
44
    /**
45
     * Get all of the created connections.
46
     *
47
     * @return array
48
     */
49 4
    public function getConnections()
50
    {
51 4
        return $this->getDrivers();
52
    }
53
54
    /**
55
     * Get the default connection name.
56
     *
57
     * @return string
58
     */
59 12
    public function getDefaultConnection()
60
    {
61 12
        return $this->app['config']['hashid.default'];
62
    }
63
64
    /**
65
     * Set the default connection name.
66
     *
67
     * @param  string  $name
68
     * @return $this
69
     */
70 4
    public function setDefaultConnection($name)
71
    {
72 4
        $this->app['config']['hashid.default'] = $name;
73
74 4
        return $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 4
    public function getDefaultDriver()
81
    {
82 4
        return $this->getDefaultConnection();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 44
    protected function createDriver($name)
89
    {
90 44
        $config = Arr::get($this->app['config']['hashid.connections'], $name, []);
91
92 44
        return $this->createForConnection($name, $config) ?:
93 44
            $this->createForDriver(Arr::pull($config, 'driver', $name), $config);
94
    }
95
96
    /**
97
     * Create a new driver instance for the given connection.
98
     *
99
     * @param  string  $name
100
     * @param  array  $config
101
     * @return mixed
102
     */
103 44
    protected function createForConnection($name, array $config = [])
104
    {
105 44
        if (isset($this->customCreators[$name])) {
106 28
            return $this->callCustom($name, compact('config'));
107
        }
108 20
    }
109
110
    /**
111
     * Create a new driver instance for the given driver.
112
     *
113
     * We will check to see if a creator method exists for the given driver,
114
     * and will call the Closure if so, which allows us to have a more generic
115
     * resolver for the drivers themselves which applies to all connections.
116
     *
117
     * @param  string  $driver
118
     * @param  array  $config
119
     * @return mixed
120
     *
121
     * @throws \InvalidArgumentException
122
     */
123 20
    protected function createForDriver($driver, array $config = [])
124
    {
125 20
        if (isset($this->customCreators[$driver])) {
126 4
            return $this->callCustom($driver, compact('config'));
127
        }
128
129 16
        if ($binding = $this->getBindingKeyForDriver($driver)) {
130 12
            return $this->resolveBinding($binding, compact('config'));
131
        }
132
133 4
        throw new InvalidArgumentException("Unsupported driver [$driver]");
134
    }
135
136
    /**
137
     * Call a custom creator.
138
     *
139
     * @param  string  $key
140
     * @param  array  $parameters
141
     * @return mixed
142
     */
143 28
    protected function callCustom($key, array $parameters = [])
144
    {
145 28
        return $this->app->call($this->customCreators[$key], $parameters);
146
    }
147
148
    /**
149
     * Get the binding key for the driver.
150
     *
151
     * @param  string  $driver
152
     * @return string|null
153
     */
154 16
    protected function getBindingKeyForDriver($driver)
155
    {
156 16
        if (class_exists($driver)) {
157 4
            return $driver;
158
        }
159
160 12
        if ($this->app->bound($key = "hashid.driver.$driver")) {
161 8
            return $key;
162
        }
163 4
    }
164
165
    /**
166
     * Resolve the given binding from the container.
167
     *
168
     * NOTE:
169
     * `Container::make($abstract, $parameters)` which can pass additional
170
     * parameters to the constructor was removed in Laravel 5.4
171
     * (https://github.com/laravel/internals/issues/391), but then re-added
172
     * as `makeWith()` in v5.4.16 (https://github.com/laravel/framework/pull/18271).
173
     * And in L55 the `makeWith()` is just an alias to `make()`.
174
     *
175
     * @param  string  $key
176
     * @param  array  $parameters
177
     * @return mixed
178
     */
179 12
    protected function resolveBinding($key, array $parameters = [])
180
    {
181 12
        if ($this->app->isShared($key)) {
182 4
            return $this->app->make($key);
183
        }
184
185 8
        $makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make';
186
187 8
        return $this->app->$makeWith($key, $parameters);
188
    }
189
}
190