Completed
Push — master ( 58b0ea...d519f2 )
by Elf
01:29
created

HashidManager::resolveFromContainer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
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 11
    public function connection($name = null)
18
    {
19 11
        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 3
    public function getDefaultConnection()
38
    {
39 3
        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 11
    protected function createDriver($name)
67
    {
68 11
        $config = $this->configuration($name);
69
70 11
        if (isset($this->customCreators[$name])) {
71 5
            return $this->callCustomCreator($name, compact('config'));
72
        }
73
74 6
        if (is_null($driver = Arr::pull($config, 'driver'))) {
75 1
            throw new InvalidArgumentException('A driver must be specified.');
76
        }
77
78 5
        return $this->createConnectionForDriver($driver, $config);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 6
    protected function callCustomCreator($driver)
85
    {
86 6
        return $this->app->call(
87 6
            $this->customCreators[$driver],
88 6
            func_num_args() > 1 ? func_get_arg(1) : []
89 6
        );
90
    }
91
92
    /**
93
     * Get the configuration for a connection.
94
     *
95
     * @param  string  $name
96
     * @return array
97
     */
98 11
    protected function configuration($name)
99
    {
100 11
        return Arr::get($this->app['config']['hashid.connections'], $name, []);
101
    }
102
103
    /**
104
     * Create a new hashid connection instance for the driver.
105
     *
106
     * We will check to see if a creator method exists for the given driver,
107
     * and will call the Closure if so, which allows us to have a more generic
108
     * resolver for the drivers themselves which applies to all connections.
109
     *
110
     * @param  string  $driver
111
     * @param  array  $config
112
     * @return mixed
113
     *
114
     * @throws \InvalidArgumentException
115
     */
116 5
    protected function createConnectionForDriver($driver, array $config = [])
117
    {
118 5
        if (isset($this->customCreators[$driver])) {
119 1
            return $this->callCustomCreator($driver, compact('config'));
120
        }
121
122 4
        if ($this->app->bound($key = "hashid.driver.{$driver}")) {
123 3
            return $this->resolveFromContainer($key, compact('config'));
124
        }
125
126 1
        throw new InvalidArgumentException("Unsupported driver [$driver]");
127
    }
128
129
    /**
130
     * Resolve the given type from the container.
131
     *
132
     * NOTE:
133
     * `Container::make($abstract, $parameters)` which can pass additional
134
     * parameters to the constructor was removed in Laravel 5.4
135
     * (https://github.com/laravel/internals/issues/391), but then re-added
136
     * as `makeWith()` in v5.4.16 (https://github.com/laravel/framework/pull/18271).
137
     * And in L55 the `makeWith()` is just an alias to `make()`.
138
     *
139
     * @param  string  $abstract
140
     * @param  array  $parameters
141
     * @return mixed
142
     */
143 3
    protected function resolveFromContainer($abstract, array $parameters = [])
144
    {
145 3
        if ($this->app->isShared($abstract)) {
146 1
            return $this->app->make($abstract);
147
        }
148
149 2
        $makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make';
150
151 2
        return $this->app->{$makeWith}($abstract, $parameters);
152
    }
153
}
154