Completed
Push — master ( 579a72...7c624b )
by Elf
01:50
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 10
    public function connection($name = null)
18
    {
19 10
        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 2
    public function getDefaultConnection()
38
    {
39 2
        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 1
    public function getDefaultDriver()
59
    {
60 1
        return $this->getDefaultConnection();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 10
    protected function createDriver($name)
67
    {
68 10
        $config = $this->configuration($name);
69
70 10
        if (isset($this->customCreators[$name])) {
71 5
            return $this->customCreators[$name]($config, $this->app, $name);
72
        }
73
74 5
        $driver = Arr::pull($config, 'driver');
75
76 5
        return $this->createConnectionForDriver($name, $driver, $config);
77
    }
78
79
    /**
80
     * Get the configuration for a connection.
81
     *
82
     * @param  string  $name
83
     * @return array
84
     */
85 10
    protected function configuration($name)
86
    {
87 10
        return Arr::get($this->app['config']['hashid.connections'], $name, []);
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  $name
98
     * @param  string  $driver
99
     * @param  array  $config
100
     * @return mixed
101
     *
102
     * @throws \InvalidArgumentException
103
     */
104 5
    protected function createConnectionForDriver($name, $driver, array $config = [])
105
    {
106 5
        if (is_null($driver)) {
107 1
            throw new InvalidArgumentException('A driver must be specified.');
108
        }
109
110 4
        if (isset($this->customCreators[$driver])) {
111 1
            return $this->customCreators[$driver]($config, $this->app, $name);
112
        }
113
114 3
        if ($this->app->bound($key = "hashid.driver.{$driver}")) {
115 2
            return $this->resolveFromContainer($key, [
116 2
                'app' => $this->app,
117 2
                'config' => $config,
118 2
            ]);
119
        }
120
121 1
        throw new InvalidArgumentException("Unsupported driver [$driver]");
122
    }
123
124
    /**
125
     * Resolve the given type from the container.
126
     *
127
     * NOTE:
128
     * `Container::make($abstract, $parameters)` which can pass additional
129
     * parameters to the constructor was removed in Laravel 5.4
130
     * (https://github.com/laravel/internals/issues/391), but then re-added
131
     * as `makeWith()` in v5.4.16 (https://github.com/laravel/framework/pull/18271).
132
     * And in L55 the `makeWith()` is just an alias to `make()`.
133
     *
134
     * @param  string  $abstract
135
     * @param  array  $parameters
136
     * @return mixed
137
     */
138 2
    protected function resolveFromContainer($abstract, array $parameters = [])
139
    {
140 2
        if ($this->app->isShared($abstract)) {
141 1
            return $this->app->make($abstract);
142
        }
143
144 1
        $makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make';
145
146 1
        return $this->app->{$makeWith}($abstract, $parameters);
147
    }
148
}
149