Completed
Push — master ( 18fc00...ff15a0 )
by Elf
01:24
created

HashidManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 131
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A connection() 0 4 1
A getConnections() 0 4 1
A getDefaultConnection() 0 4 1
A setDefaultConnection() 0 6 1
A getDefaultDriver() 0 4 1
B createDriver() 0 22 4
A getConfigWithoutDriver() 0 6 1
B createConnection() 0 29 5
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use InvalidArgumentException;
6
use Illuminate\Support\Manager;
7
8
class HashidManager extends Manager
9
{
10
    /**
11
     * Get a hashid connection instance.
12
     *
13
     * @param  string|null  $name
14
     * @return mixed
15
     */
16 10
    public function connection($name = null)
17
    {
18 10
        return $this->driver($name);
19
    }
20
21
    /**
22
     * Get all of the created connections.
23
     *
24
     * @return array
25
     */
26 1
    public function getConnections()
27
    {
28 1
        return $this->getDrivers();
29
    }
30
31
    /**
32
     * Get the default connection name.
33
     *
34
     * @return string
35
     */
36 2
    public function getDefaultConnection()
37
    {
38 2
        return $this->app['config']['hashid.default'];
39
    }
40
41
    /**
42
     * Set the default connection name.
43
     *
44
     * @param  string  $name
45
     * @return $this
46
     */
47 1
    public function setDefaultConnection($name)
48
    {
49 1
        $this->app['config']['hashid.default'] = $name;
50
51 1
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function getDefaultDriver()
58
    {
59 1
        return $this->getDefaultConnection();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 10
    protected function createDriver($name)
66
    {
67 10
        $config = $this->app['config']->get("hashid.connections.{$name}", []);
68
69
        // First we will check by the connection name to see if a creator method
70
        // exists for the given connection.
71 10
        if (isset($this->customCreators[$name])) {
72 5
            return $this->customCreators[$name]($this->app, $config, $name);
73
        }
74
75
        // Next we will check by the driver name to see if a creator method
76
        // exists for the given driver, and will call the Closure if so,
77
        // which allows us to have a more generic resolver for the drivers
78
        // themselves which applies to all connections.
79 5
        if (isset($config['driver']) &&
80 4
            isset($this->customCreators[$driver = $config['driver']])
81 5
        ) {
82 1
            return $this->customCreators[$driver]($this->app, $this->getConfigWithoutDriver($config), $name);
83
        }
84
85 4
        return $this->createConnection($config);
86
    }
87
88
    /**
89
     * Create a new hashid connection instance.
90
     *
91
     * @param  array  $config
92
     * @return mixed
93
     *
94
     * @throws \InvalidArgumentException
95
     */
96 4
    protected function createConnection(array $config)
97
    {
98 4
        if (! isset($config['driver'])) {
99 1
            throw new InvalidArgumentException('A driver must be specified.');
100
        }
101
102 3
        $driver = $config['driver'];
103
104 3
        if ($this->app->bound($key = "hashid.connection.{$driver}")) {
105 2
            if ($this->app->isShared($key)) {
106 1
                return $this->app->make($key);
107
            }
108
109
            // `Container::make($abstract, $parameters)` which can
110
            // pass additional parameters to the constructor was removed
111
            // in Laravel 5.4 (https://github.com/laravel/internals/issues/391),
112
            // but then re-added as `makeWith()` in v5.4.16
113
            // (https://github.com/laravel/framework/pull/18271). And in L55
114
            // `makeWith()` is just an alias to `make()`.
115 1
            $makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make';
116
117 1
            return $this->app->{$makeWith}($key, [
118 1
                'app' => $this->app,
119 1
                'config' => $this->getConfigWithoutDriver($config),
120 1
            ]);
121
        }
122
123 1
        throw new InvalidArgumentException("Unsupported driver [$driver]");
124
    }
125
126
    /**
127
     * Get connection configuration without "driver" element.
128
     *
129
     * @param  array  $config
130
     * @return array
131
     */
132 2
    protected function getConfigWithoutDriver($config)
133
    {
134 2
        unset($config['driver']);
135
136 2
        return $config;
137
    }
138
}
139