Completed
Push — master ( dd633f...1cad5e )
by Ben
02:28
created

ServiceProvider::selectStorage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Benrowe\Laravel\Config;
4
5
use Benrowe\Laravel\Config\Storage\File;
6
use Benrowe\Laravel\Config\Storage\Pdo;
7
use Benrowe\Laravel\Config\Storage\Redis;
8
use Benrowe\Laravel\Config\Storage\StorageInterface;
9
use Illuminate\Contracts\Config\Repository as Config;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Benrowe\Laravel\Config\Config.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
11
12
/**
13
 * Service Provider for Config
14
 *
15
 * @package Benrowe\Laravel\Config;
16
 */
17
class ServiceProvider extends BaseServiceProvider
18
{
19
    protected $defer = false;
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function boot()
25
    {
26
        # publish necessary files
27
        $this->publishes([
28
            __DIR__ . '/config/config.php' => config_path('config.php'),
29
        ], 'config');
30
31
        $this->publishes([
32
            __DIR__.'/migrations/' => database_path('migrations'),
33
        ], 'migrations');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function register()
40
    {
41
        $this->app->singleton('config-runtime', function () {
42
            $storage = $this->selectStorage($this->app['config']);
43
            if ($storage === null) {
44
                $storage = [];
45
            }
46
            $cfg = new Config($storage);
0 ignored issues
show
Unused Code introduced by
The call to Repository::__construct() has too many arguments starting with $storage.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
            $modifiers = $this->app['config']->get('config.modifiers', []);
48
            foreach ($modifiers as $className) {
49
                $cfg->modifiers->push(new $className);
0 ignored issues
show
Bug introduced by
Accessing modifiers on the interface Illuminate\Contracts\Config\Repository suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
50
            }
51
52
            return $cfg;
53
        });
54
    }
55
56
    /**
57
     * Define the services this provider will build & provide
58
     *
59
     * @return string[]
60
     */
61
    public function provides()
62
    {
63
        return [
64
            'Benrowe\Laravel\Config\Config',
65
            'config-runtime'
66
        ];
67
    }
68
69
    /**
70
     * Retrieve the storage engine, based on the current configuration
71
     *
72
     * @param  Config $config
73
     * @return StorageInterface
74
     */
75
    protected function selectStorage($config)
76
    {
77
        if (!$config->get('config.storage.enabled')) {
78
            return null;
79
        }
80
        $driver = $config->get('config.storage.driver', 'file');
81
        $method = 'selectStorage'.ucfirst($driver);
82
        return $this->$method($config);
83
    }
84
85
    /**
86
     * Get an instance of the PDO storage engine
87
     *
88
     * @param  Config $config
89
     * @return Pdo
90
     */
91
    protected function selectStoragePdo($config)
92
    {
93
        $connection = $config->get('config.storage.connection');
94
        $table = $this->app['db']->getTablePrefix() . 'config';
95
        $pdo = $this->app['db']->connection($connection)->getPdo();
96
        return new Pdo($pdo, $table);
97
    }
98
99
    /**
100
     * Get an instance of the Redis storage engine
101
     *
102
     * @param  Config  $config
103
     * @return Redis
104
     */
105
    protected function selectStorageRedis($config)
106
    {
107
        $connection = $config->get('config.storage.connection');
108
        return new Redis($this->app['redis']->connection($connection));
109
    }
110
111
    /**
112
     * Get an instance of a custom defined storage engine
113
     *
114
     * @param  Config $config [description]
115
     * @return StorageInterface
116
     */
117
    protected function selectStorageCustom($config)
118
    {
119
        $class = $config->get('config.storage.provider');
120
        return $this->app->make($class);
121
    }
122
123
    /**
124
     * Get an instance of the File storage engine
125
     *
126
     * @param  Config $config
127
     * @return File
128
     */
129
    protected function selectStorageFile($config)
130
    {
131
        $path = $config->get('config.storage.path');
132
        return new File($path);
133
    }
134
}
135