RepositoryServiceProvider   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 44
c 4
b 2
f 0
dl 0
loc 128
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerCriteriaGenerator() 0 8 1
A findDefaultImplementation() 0 10 3
A register() 0 13 1
B autoBindRepositories() 0 41 8
A registerRepositoryGenerator() 0 8 1
A boot() 0 5 1
1
<?php
2
3
namespace OkayBueno\Repositories;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Support\Str;
7
8
/**
9
 * Class RepositoriesServiceProvider
10
 * @package OkayBueno\Repositories
11
 */
12
class RepositoryServiceProvider extends ServiceProvider
13
{
14
    
15
    private $configPath = '/config/repositories.php';
16
    
17
    /**
18
     *
19
     */
20
    public function boot()
21
    {
22
        $this->publishes([
23
            __DIR__.$this->configPath => config_path('repositories.php'),
0 ignored issues
show
Bug introduced by
The function config_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
            __DIR__.$this->configPath => /** @scrutinizer ignore-call */ config_path('repositories.php'),
Loading history...
24
        ], 'repositories');
25
    }
26
    
27
    
28
    /**
29
     *
30
     */
31
    public function register()
32
    {
33
        // merge default config
34
        $this->mergeConfigFrom(
35
            __DIR__.$this->configPath , 'repositories'
36
        );
37
        
38
        // Bind the repositories.
39
        $this->autoBindRepositories();
40
        
41
        // And generators.
42
        $this->registerRepositoryGenerator();
43
        $this->registerCriteriaGenerator();
44
    }
45
    
46
    
47
    /**
48
     *
49
     */
50
    private function autoBindRepositories()
51
    {
52
        // Load config parameters needed.
53
        $repositoriesBasePath = config( 'repositories.repositories_path' );
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        $repositoriesBasePath = /** @scrutinizer ignore-call */ config( 'repositories.repositories_path' );
Loading history...
54
        $baseNamespace = rtrim( config( 'repositories.repository_interfaces_namespace' ), '\\' ) . '\\';
55
        $skipRepositories = config( 'repositories.skip' );
56
        $implementationBindings = config( 'repositories.bindings' );
57
        $defaultImplementation = $this->findDefaultImplementation( $implementationBindings );
58
        
59
        if ( \File::exists( $repositoriesBasePath ) )
60
        {
61
            $allRepos = \File::files( $repositoriesBasePath );
62
            
63
            foreach( $allRepos as $repo )
64
            {
65
                $implementation = $defaultImplementation;
66
                $interfaceName = pathinfo( $repo, PATHINFO_FILENAME );
67
                if ( in_array( $interfaceName, $skipRepositories ) ) continue;
68
                else
69
                {
70
                    $commonName = str_replace( 'Interface', '', $interfaceName );
71
                    $interfaceFullClassName = $baseNamespace.$interfaceName;
72
                    
73
                    foreach( $implementationBindings as $engine => $bindRepositories )
74
                    {
75
                        if ( $bindRepositories === 'default' ) continue;
76
                        else if ( in_array( $interfaceName, $bindRepositories ) )
77
                        {
78
                            $implementation = $engine;
79
                            break;
80
                        }
81
                    }
82
                    
83
                    $fullClassName = $baseNamespace.ucfirst( Str::camel( $implementation ) ).'\\'.$commonName;
84
                    
85
                    if ( class_exists( $fullClassName ) )
86
                    {
87
                        // Bind the class.
88
                        $this->app->bind( $interfaceFullClassName, function ( $app ) use ( $fullClassName )
89
                        {
90
                            return $app->make( $fullClassName );
91
                        });
92
                    }
93
                }
94
            }
95
        }
96
        
97
    }
98
    
99
    /**
100
     * @param $implementations
101
     * @return array|mixed|string
102
     */
103
    private function findDefaultImplementation( $implementations )
104
    {
105
        $filtered = array_filter( $implementations, function( $k ) {
106
            return $k === 'default';
107
        });
108
        
109
        $default = array_keys($filtered);
110
        $default = is_array( $default ) ? $default[0] : $default;
0 ignored issues
show
introduced by
The condition is_array($default) is always true.
Loading history...
111
        
112
        return $default ? $default : 'eloquent';
113
    }
114
    
115
    /**
116
     *
117
     */
118
    private function registerRepositoryGenerator()
119
    {
120
        $this->app->singleton('command.repository', function ($app)
121
        {
122
            return $app['OkayBueno\Repositories\Commands\MakeRepositoryCommand'];
123
        });
124
        
125
        $this->commands('command.repository');
126
    }
127
    
128
    
129
    /**
130
     *
131
     */
132
    private function registerCriteriaGenerator()
133
    {
134
        $this->app->singleton('command.criteria', function ($app)
135
        {
136
            return $app['OkayBueno\Repositories\Commands\MakeCriteriaCommand'];
137
        });
138
        
139
        $this->commands('command.criteria');
140
    }
141
}
142