Completed
Push — master ( 77f35f...54e82c )
by Elf
01:42
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 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 9
    public function connection($name = null)
17
    {
18 9
        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 9
    protected function createDriver($name)
66
    {
67 9
        $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 9
        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 4
        if (isset($config['driver']) &&
80 3
            isset($this->customCreators[$driver = $config['driver']])
81 4
        ) {
82 1
            return $this->customCreators[$driver]($this->app, $this->getConfigWithoutDriver($config), $name);
83
        }
84
85 3
        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 3
    protected function createConnection(array $config)
97
    {
98 3
        if (! isset($config['driver'])) {
99 1
            throw new InvalidArgumentException('A driver must be specified.');
100
        }
101
102 2
        $driver = $config['driver'];
103
104 2
        if ($this->app->bound($key = "hashid.connection.{$driver}")) {
105 1
            if ($this->app->isShared($key)) {
106
                return $this->app->make($key);
107
            }
108
109 1
            $make = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make';
110
111 1
            return $this->app->{$make}($key, [
112 1
                'app' => $this->app,
113 1
                'config' => $this->getConfigWithoutDriver($config),
114 1
            ]);
115
        }
116
117 1
        throw new InvalidArgumentException("Unsupported driver [$driver]");
118
    }
119
120
    /**
121
     * Get connection configuration without "driver" element.
122
     *
123
     * @param  array  $config
124
     * @return array
125
     */
126 2
    protected function getConfigWithoutDriver($config)
127
    {
128 2
        unset($config['driver']);
129
130 2
        return $config;
131
    }
132
}
133