Test Failed
Pull Request — stable (#304)
by
unknown
07:10 queued 05:13
created

BuiltServiceProvider::getDefaultConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
4
/**
5
 * This file is part of Laravel Zero.
6
 *
7
 * (c) Nuno Maduro <[email protected]>
8
 *
9
 *  For the full copyright and license information, please view the LICENSE
10
 *  file that was distributed with this source code.
11
 */
12
13
namespace LaravelZero\Framework\Providers\Built;
14
15
use Illuminate\Support\Str;
16
use Illuminate\Support\Facades\Config;
17
use Illuminate\Support\ServiceProvider;
18
19
class BuiltServiceProvider extends ServiceProvider
20
{
21
    const CONFIG_KEY = 'app-built';
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 32
    public function boot(): void
27
    {
28 32
        if ($this->canReplace('filesystems')) {
29 1
            $this->replaceFileSystems();
30
        }
31 32
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 32
    public function register(): void
37
    {
38 32
        $config = $this->app->make('config');
39
40 32
        if ($config->get(self::CONFIG_KEY) === null) {
41 32
            $config->set(self::CONFIG_KEY, $this->getDefaultConfig());
42
        }
43 32
    }
44
45
    /**
46
     * Replace keys inside the filesystems config.
47
     */
48 1
    protected function replaceFileSystems(): void
49
    {
50 1
        $currentFileSystems = config('filesystems');
51
52 1
        if (isset($currentFileSystems['disks']) && count($currentFileSystems['disks']) > 0) {
53 1
            if ($this->canReplace('filesystems.drivers')) {
54 1
                foreach ($this->get('filesystems.drivers') as $driver => $replace) {
55 1
                    foreach ($replace as $key => $replacements) {
56 1
                        $old = array_shift($replacements);
57 1
                        $new = array_shift($replacements);
58
59 1
                        if (! empty($old) && ! empty($new)) {
60 1
                            foreach ($currentFileSystems['disks'] as $diskName => $diskConfig) {
61 1
                                if ($diskConfig['driver'] === $driver && Str::startsWith($diskConfig[$key], $old)) {
62 1
                                    $diskConfig[$key] = str_replace($old, $new, $diskConfig[$key]);
63 1
                                    Config::set('filesystems.disks.'.$diskName, $diskConfig);
64
                                }
65
                            }
66
                        }
67
                    }
68
                }
69
            }
70
        }
71 1
    }
72
73
    /**
74
     * Verify if we can replace settings when app is in production.
75
     *
76
     * @param null $what
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $what is correct as it would always require null to be passed?
Loading history...
77
     * @return bool
78
     */
79 32
    protected function canReplace($what = null): bool
80
    {
81 32
        $canReplace = true;
82 32
        if (! is_null($what)) {
0 ignored issues
show
introduced by
The condition is_null($what) is always true.
Loading history...
83 32
            if (is_null(config(self::CONFIG_KEY.'.'.$what))) {
84
                $canReplace = false;
85
            }
86
        }
87
88 32
        return config('app.production') === true && $canReplace;
89
    }
90
91
    /**
92
     * @param null $what
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $what is correct as it would always require null to be passed?
Loading history...
93
     *
94
     * @return \Illuminate\Config\Repository|mixed|null
95
     */
96 1
    protected function get($what = null)
97
    {
98 1
        if (is_null($what)) {
0 ignored issues
show
introduced by
The condition is_null($what) is always true.
Loading history...
99
            return config(self::CONFIG_KEY);
100
        }
101
102 1
        return config(self::CONFIG_KEY.'.'.$what);
103
    }
104
105
    /**
106
     * Returns the default application build config.
107
     */
108 32
    protected function getDefaultConfig(): array
109
    {
110
        return [
111
            'filesystems' => [
112
                'drivers' => [
113
                    'local' => [
114
                        'root' => [
115 32
                            $this->app->storagePath(),
116 32
                            rtrim(getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'storage',
117
                        ],
118
                    ],
119
                ],
120
            ],
121
        ];
122
    }
123
}
124