Completed
Branch master (949156)
by Elf
01:16
created

HashidManager::createDriver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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