ConfigServiceProvider::boot()   F
last analyzed

Complexity

Conditions 12
Paths 768

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 16.9113

Importance

Changes 0
Metric Value
cc 12
nc 768
nop 0
dl 0
loc 74
ccs 25
cts 37
cp 0.6757
crap 16.9113
rs 3.065
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of Cachet.
5
 *
6
 * (c) Alt Three Services Limited
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CachetHQ\Cachet\Foundation\Providers;
13
14
use CachetHQ\Cachet\Models\Setting as SettingModel;
15
use CachetHQ\Cachet\Settings\Cache;
16
use CachetHQ\Cachet\Settings\Repository;
17
use Exception;
18
use Illuminate\Support\ServiceProvider;
19
use Jenssegers\Date\Date;
20
21
/**
22
 * This is the config service provider class.
23
 *
24
 * @author James Brooks <[email protected]>
25
 * @author Graham Campbell <[email protected]>
26
 * @author Joseph Cohen <[email protected]>
27
 */
28
class ConfigServiceProvider extends ServiceProvider
29
{
30
    /**
31
     * Boot the service provider.
32
     *
33
     * @return void
34
     */
35 603
    public function boot()
36
    {
37 603
        $env = $this->app->environment();
38 603
        $repo = $this->app->make(Repository::class);
39 603
        $cache = $this->app->make(Cache::class);
40 603
        $loaded = $cache->load($env);
41
42
        $this->app->terminating(function () use ($repo, $cache) {
0 ignored issues
show
Bug introduced by Graham Campbell
The method terminating does only exist in Illuminate\Foundation\Application, but not in Illuminate\Contracts\Foundation\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43 94
            if ($repo->stale()) {
44 7
                $cache->clear();
45
            }
46 603
        });
47
48
        try {
49 603
            if ($loaded === false) {
50 603
                $loaded = $repo->all();
51
                $cache->store($env, $loaded);
52
            }
53
54
            $settings = array_merge($this->app->config->get('setting'), $loaded);
55
56
            $this->app->config->set('setting', $settings);
57 603
        } catch (Exception $e) {
58
            //
59
        }
60
61
        // Set the app url.
62 603
        if ($appDomain = $this->app->config->get('setting.app_domain')) {
63
            $this->app->config->set('app.url', $appDomain);
64
        }
65
66
        // Set the locale.
67 603
        if ($appLocale = $this->app->config->get('setting.app_locale')) {
68
            $this->app->config->set('app.locale', $appLocale);
69
            $this->app->translator->setLocale($appLocale);
70
            Date::setLocale($appLocale);
71
        }
72
73
        // Set the timezone.
74 603
        if ($appTimezone = $this->app->config->get('setting.app_timezone')) {
75
            $this->app->config->set('cachet.timezone', $appTimezone);
76
        }
77
78
        // Set allowed domains for CORS.
79 603
        $allowedOrigins = $this->app->config->get('cors.defaults.allowedOrigins');
80
81 603
        if ($allowedDomains = $this->app->config->get('setting.allowed_domains')) {
82
            $domains = explode(',', $allowedDomains);
83
            foreach ($domains as $domain) {
84
                $allowedOrigins[] = $domain;
85
            }
86
        } else {
87 603
            $allowedOrigins[] = $this->app->config->get('app.url');
88
        }
89
90 603
        $this->app->config->set('cors.paths.api/v1/*.allowedOrigins', $allowedOrigins);
91
92
        // Set the mail from address.
93 603
        if (!$this->app->config->get('mail.from.address')) {
94 603
            $url = parse_url($appDomain);
95
96 603
            if (isset($url['host'])) {
97
                $this->app->config->set('mail.from.address', "[email protected]{$url['host']}");
98
            }
99
        }
100
101
        // Set the mail from name.
102 603
        if (!$this->app->config->get('mail.from.name')) {
103 603
            $this->app->config->set(
104 603
                'mail.from.name',
105 603
                $this->app->config->get('setting.app_name', $this->app->config->get('app.name'))
106
            );
107
        }
108
    }
109
110
    /**
111
     * Register the service provider.
112
     *
113
     * @return void
114
     */
115 603
    public function register()
116
    {
117
        $this->app->singleton(Cache::class, function ($app) {
118 603
            return new Cache($app->files, $app->bootstrapPath().'/cachet');
119 603
        });
120
121
        $this->app->singleton(Repository::class, function () {
122 603
            return new Repository(new SettingModel());
123 603
        });
124
    }
125
}
126