Completed
Push — master ( 7cf8d9...f10042 )
by Patrick
19:35 queued 04:05
created

LaravelNomadServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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