LaravelNomadServiceProvider::register()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
c 2
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ShiftOneLabs\LaravelNomad;
4
5
use LogicException;
6
use Illuminate\Support\Str;
7
use Illuminate\Database\Connection;
8
use Illuminate\Support\ServiceProvider;
9
10
class LaravelNomadServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * The connection classes to use for the drivers.
14
     *
15
     * @var array
16
     */
17
    protected $classes = [
18
        'mysql' => 'ShiftOneLabs\LaravelNomad\Extension\Database\MySqlConnection',
19
        'pgsql' => 'ShiftOneLabs\LaravelNomad\Extension\Database\PostgresConnection',
20
        'sqlite' => 'ShiftOneLabs\LaravelNomad\Extension\Database\SQLiteConnection',
21
        'sqlsrv' => 'ShiftOneLabs\LaravelNomad\Extension\Database\SqlServerConnection',
22
    ];
23
24
    /**
25
     * Register any application services.
26
     *
27
     * @return void
28
     */
29 2613
    public function register()
30
    {
31 2613
        $this->registerFeatureDetection();
32
33 2613
        $this->registerConnections();
34 2613
    }
35
36
    /**
37
     * Register the feature detection service.
38
     *
39
     * @return void
40
     */
41 2613
    public function registerFeatureDetection()
42
    {
43 2613
        $this->app->singleton('nomad.feature.detection', 'ShiftOneLabs\LaravelNomad\FeatureDetection');
44 2613
    }
45
46
    /**
47
     * Register the database connection extensions.
48
     *
49
     * @return void
50
     */
51 2613
    public function registerConnections()
52
    {
53 2613
        $driver = $this->app['nomad.feature.detection']->connectionResolverDriver();
54
55 2613
        $method = 'registerVia'.Str::studly($driver);
56
57 2613
        if (method_exists($this, $method)) {
58 2613
            return $this->{$method}();
59
        }
60
61 67
        throw new LogicException(sprintf('Connection registration method [%s] does not exist.', $method));
62
    }
63
64
    /**
65
     * Register the database connection extensions through app bindings.
66
     *
67
     * @return void
68
     */
69 1599
    public function registerViaBindings()
70
    {
71 1599
        foreach ($this->classes as $driver => $class) {
72 1599
            $this->app->bind('db.connection.'.$driver, $class);
73 390
        }
74 1599
    }
75
76
    /**
77
     * Register the database connection extensions through the `Connection` method.
78
     *
79
     * @return void
80
     */
81 1014
    public function registerViaConnectionMethod()
82
    {
83 1014
        foreach ($this->classes as $driver => $class) {
84 1014
            Connection::resolverFor($driver, function ($connection, $database, $prefix, $config) use ($class) {
85 728
                return new $class($connection, $database, $prefix, $config);
86 1014
            });
87 39
        }
88 1014
    }
89
}
90