Completed
Push — master ( 510e41...9182df )
by Elf
04:54 queued 21s
created

HashidManager::setDefaultConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
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
            return $this->resolveFromContainer($binding, compact('config'));
111
        }
112
113 6
        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
        return Arr::first(
125 6
            [$driver, 'hashid.driver.'.$driver],
126 6
            [$this->app, 'bound']
127 6
        );
128
    }
129
130
    /**
131
     * Resolve the given binding from the container.
132
     *
133
     * NOTE:
134
     * `Container::make($abstract, $parameters)` which can pass additional
135
     * parameters to the constructor was removed in Laravel 5.4
136
     * (https://github.com/laravel/internals/issues/391), but then re-added
137
     * as `makeWith()` in v5.4.16 (https://github.com/laravel/framework/pull/18271).
138
     * And in L55 the `makeWith()` is just an alias to `make()`.
139
     *
140
     * @param  string  $abstract
141
     * @param  array  $parameters
142
     * @return mixed
143
     */
144
    protected function resolveFromContainer($abstract, array $parameters = [])
145
    {
146
        if ($this->app->isShared($abstract)) {
147
            return $this->app->make($abstract);
148
        }
149
150
        $makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make';
151
152
        return $this->app->$makeWith($abstract, $parameters);
153
    }
154
155
    /**
156
     * Get the configuration for a connection.
157
     *
158
     * @param  string  $name
159
     * @return array
160
     */
161 13
    protected function configuration($name)
162
    {
163 13
        return Arr::get($this->app['config']['hashid.connections'], $name, []);
164
    }
165
}
166