ConfigureSystem::handle()   C
last analyzed

Complexity

Conditions 18
Paths 180

Size

Total Lines 58
Code Lines 27

Duplication

Lines 36
Ratio 62.07 %

Importance

Changes 0
Metric Value
dl 36
loc 58
c 0
b 0
f 0
rs 5.8098
cc 18
eloc 27
nc 180
nop 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 namespace Anomaly\PreferencesModule\Preference\Listener;
2
3
use Anomaly\PreferencesModule\Preference\Contract\PreferenceRepositoryInterface;
4
use Anomaly\Streams\Platform\Addon\Addon;
5
use Anomaly\Streams\Platform\Addon\AddonCollection;
6
use Illuminate\Contracts\Config\Repository;
7
8
/**
9
 * Class ConfigureSystem
10
 *
11
 * @link          http://pyrocms.com/
12
 * @author        PyroCMS, Inc. <[email protected]>
13
 * @author        Ryan Thompson <[email protected]>
14
 */
15
class ConfigureSystem
16
{
17
18
    /**
19
     * The addon collection.
20
     *
21
     * @var AddonCollection
22
     */
23
    protected $addons;
24
25
    /**
26
     * The config repository.
27
     *
28
     * @var Repository
29
     */
30
    protected $config;
31
32
    /**
33
     * The preferences repository.
34
     *
35
     * @var PreferenceRepositoryInterface
36
     */
37
    protected $preferences;
38
39
    /**
40
     * Create a new ConfigureSystem instance.
41
     *
42
     * @param PreferenceRepositoryInterface $preferences
43
     * @param AddonCollection               $addons
44
     * @param Repository                    $config
45
     */
46
    public function __construct(PreferenceRepositoryInterface $preferences, AddonCollection $addons, Repository $config)
47
    {
48
        $this->config      = $config;
49
        $this->addons      = $addons;
50
        $this->preferences = $preferences;
51
    }
52
53
54
    /**
55
     * Handle the event.
56
     */
57
    public function handle()
58
    {
59
        /* @var Addon $addon */
60 View Code Duplication
        foreach ($this->addons->withConfig('preferences') as $addon) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            foreach ($this->config->get($addon->getNamespace('preferences')) as $key => $setting) {
62
63
                if (isset($setting['env']) && env($setting['env']) !== null) {
64
                    continue;
65
                }
66
67
                if (!isset($setting['bind'])) {
68
                    continue;
69
                }
70
71
                if (!$this->preferences->has($key = $addon->getNamespace($key))) {
72
                    continue;
73
                }
74
75
                $this->config->set($setting['bind'], $this->preferences->presenter($key)->__value);
76
            }
77
        }
78
79 View Code Duplication
        foreach ($this->addons->withConfig('preferences/preferences') as $addon) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            foreach ($this->config->get($addon->getNamespace('preferences/preferences')) as $key => $setting) {
81
82
                if (isset($setting['env']) && env($setting['env']) !== null) {
83
                    continue;
84
                }
85
86
                if (!isset($setting['bind'])) {
87
                    continue;
88
                }
89
90
                if (!$this->preferences->has($key = $addon->getNamespace($key))) {
91
                    continue;
92
                }
93
94
                $this->config->set($setting['bind'], $this->preferences->presenter($key)->__value);
95
            }
96
        }
97
98
        foreach ($this->config->get('streams::preferences/preferences', []) as $key => $setting) {
99
100
            if (isset($setting['env']) && env($setting['env']) !== null) {
101
                continue;
102
            }
103
104
            if (!isset($setting['bind'])) {
105
                continue;
106
            }
107
108
            if (!$this->preferences->has($key = 'streams::' . $key)) {
109
                continue;
110
            }
111
112
            $this->config->set($setting['bind'], $this->preferences->presenter($key)->__value);
113
        }
114
    }
115
}
116