Completed
Push — master ( 8d47d0...2a8117 )
by Elf
22:10 queued 07:23
created

HashidManager::connection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 44
     */
18
    protected $app;
19 44
20
    /**
21
     * Create a new manager instance.
22
     *
23
     * @param  \Illuminate\Contracts\Container\Container  $container
24
     * @return void
25
     */
26
    public function __construct($container)
27 4
    {
28
        parent::__construct($container);
29 4
30
        $this->app = $container;
31
    }
32
33
    /**
34
     * Get a hashid connection instance.
35
     *
36
     * @param  string|null  $name
37 12
     * @return mixed
38
     */
39 12
    public function connection($name = null)
40
    {
41
        return $this->driver($name);
42
    }
43
44
    /**
45
     * Get all of the created connections.
46
     *
47
     * @return array
48 4
     */
49
    public function getConnections()
50 4
    {
51
        return $this->getDrivers();
52 4
    }
53
54
    /**
55
     * Get the default connection name.
56
     *
57
     * @return string
58 4
     */
59
    public function getDefaultConnection()
60 4
    {
61
        return $this->app['config']['hashid.default'];
62
    }
63
64
    /**
65
     * Set the default connection name.
66 44
     *
67
     * @param  string  $name
68 44
     * @return $this
69
     */
70 44
    public function setDefaultConnection($name)
71 44
    {
72
        $this->app['config']['hashid.default'] = $name;
73
74
        return $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getDefaultDriver()
81 44
    {
82
        return $this->getDefaultConnection();
83 44
    }
84 28
85
    /**
86 20
     * {@inheritdoc}
87
     */
88
    protected function createDriver($name)
89
    {
90
        $config = Arr::get($this->app['config']['hashid.connections'], $name, []);
91
92
        return $this->createForConnection($name, $config) ?:
93
            $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 20
     * @return mixed
102
     */
103 20
    protected function createForConnection($name, array $config = [])
104 4
    {
105
        if (isset($this->customCreators[$name])) {
106
            return $this->callCustom($name, compact('config'));
107 16
        }
108 12
    }
109
110
    /**
111 4
     * 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 28
     * @throws \InvalidArgumentException
122
     */
123 28
    protected function createForDriver($driver, array $config = [])
124
    {
125
        if (isset($this->customCreators[$driver])) {
126
            return $this->callCustom($driver, compact('config'));
127
        }
128
129
        if ($binding = $this->getBindingKeyForDriver($driver)) {
130
            return $this->resolveBinding($binding, compact('config'));
131
        }
132 16
133
        throw new InvalidArgumentException("Unsupported driver [$driver]");
134 16
    }
135 4
136
    /**
137
     * Call a custom creator.
138 12
     *
139 8
     * @param  string  $key
140
     * @param  array  $parameters
141 4
     * @return mixed
142
     */
143
    protected function callCustom($key, array $parameters = [])
144
    {
145
        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
    protected function getBindingKeyForDriver($driver)
155
    {
156
        if (class_exists($driver)) {
157 12
            return $driver;
158
        }
159 12
160 4
        if ($this->app->bound($key = "hashid.driver.$driver")) {
161
            return $key;
162
        }
163 8
    }
164
165 8
    /**
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
    protected function resolveBinding($key, array $parameters = [])
180
    {
181
        if ($this->app->isShared($key)) {
182
            return $this->app->make($key);
183
        }
184
185
        $makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make';
186
187
        return $this->app->$makeWith($key, $parameters);
188
    }
189
}
190