Completed
Push — master ( 279d4e...5d09e0 )
by Elf
02:49
created

configureForCurrentRequest()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 0
dl 0
loc 17
ccs 0
cts 12
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Providers;
4
5
use Illuminate\Support\ServiceProvider;
6
7
class AppConfigServiceProvider extends ServiceProvider
8
{
9
    /**
10
     * Register the service provider.
11
     */
12
    public function register()
13
    {
14
        if (! $this->app->configurationIsCached()) {
15
            $this->configureDefaults();
16
        }
17
18
        $this->configureForCurrentRequest();
19
    }
20
21
    /**
22
     * Configure app defaults.
23
     */
24
    protected function configureDefaults()
25
    {
26
        $config = $this->app['config'];
27
28
        // Append "app.domains"
29
        $config['app.domains'] = array_map(function ($value) {
30
            if (is_string($domain = parse_url($value, PHP_URL_HOST))) {
31
                if (str_contains($domain, '.')) {
32
                    return $domain;
33
                }
34
            }
35
        }, $config['support.url']);
36
37
        // Set "mail.from.name"
38
        if ($config['mail.from.name'] == 'Example') {
39
            $config['mail.from.name'] = $config['app.name'];
40
        }
41
    }
42
43
    /**
44
     * Configure app for the current request.
45
     */
46
    protected function configureForCurrentRequest()
47
    {
48
        $config = $this->app['config'];
49
        $request = $this->app['request'];
50
51
        $identifier = array_search($request->getHost(), $config['app.domains']);
52
53
        // Configure the cookie domain
54
        if (! is_null($identifier) && $config->has('support.cookie_domain.'.$identifier)) {
55
            $config['session.domain'] = $config['support.cookie_domain.'.$identifier];
56
        }
57
58
        // Configure the auth defaults
59
        if (! is_null($identifier) && is_array($auth = $config['support.auth.'.$identifier])) {
60
            $config['auth.defaults'] = $auth;
61
        }
62
    }
63
}
64