Completed
Push — master ( 89db83...723e32 )
by Ryan
02:42
created

ConfigureSystem::handle()   C

Complexity

Conditions 18
Paths 180

Size

Total Lines 58
Code Lines 27

Duplication

Lines 36
Ratio 62.07 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 36
loc 58
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\Bus\SelfHandling;
7
use Illuminate\Contracts\Config\Repository;
8
9
/**
10
 * Class ConfigureSystem
11
 *
12
 * @link          http://pyrocms.com/
13
 * @author        PyroCMS, Inc. <[email protected]>
14
 * @author        Ryan Thompson <[email protected]>
15
 * @package       Anomaly\PreferencesModule\Preference\Listener
16
 */
17
class ConfigureSystem
18
{
19
20
    /**
21
     * The addon collection.
22
     *
23
     * @var AddonCollection
24
     */
25
    protected $addons;
26
27
    /**
28
     * The config repository.
29
     *
30
     * @var Repository
31
     */
32
    protected $config;
33
34
    /**
35
     * The preferences repository.
36
     *
37
     * @var PreferenceRepositoryInterface
38
     */
39
    protected $preferences;
40
41
    /**
42
     * Create a new ConfigureSystem instance.
43
     *
44
     * @param PreferenceRepositoryInterface $preferences
45
     * @param AddonCollection               $addons
46
     * @param Repository                    $config
47
     */
48
    public function __construct(PreferenceRepositoryInterface $preferences, AddonCollection $addons, Repository $config)
49
    {
50
        $this->config      = $config;
51
        $this->addons      = $addons;
52
        $this->preferences = $preferences;
53
    }
54
55
56
    /**
57
     * Handle the event.
58
     */
59
    public function handle()
60
    {
61
        /* @var Addon $addon */
62 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...
63
            foreach ($this->config->get($addon->getNamespace('preferences')) as $key => $setting) {
64
65
                if (isset($setting['env']) && env($setting['env']) !== null) {
66
                    continue;
67
                }
68
69
                if (!isset($setting['bind'])) {
70
                    continue;
71
                }
72
73
                if (!$this->preferences->has($key = $addon->getNamespace($key))) {
74
                    continue;
75
                }
76
77
                $this->config->set($setting['bind'], $this->preferences->value($key));
78
            }
79
        }
80
81 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...
82
            foreach ($this->config->get($addon->getNamespace('preferences/preferences')) as $key => $setting) {
83
84
                if (isset($setting['env']) && env($setting['env']) !== null) {
85
                    continue;
86
                }
87
88
                if (!isset($setting['bind'])) {
89
                    continue;
90
                }
91
92
                if (!$this->preferences->has($key = $addon->getNamespace($key))) {
93
                    continue;
94
                }
95
96
                $this->config->set($setting['bind'], $this->preferences->value($key));
97
            }
98
        }
99
100
        foreach ($this->config->get('streams::preferences/preferences') as $key => $setting) {
101
102
            if (isset($setting['env']) && env($setting['env']) !== null) {
103
                continue;
104
            }
105
106
            if (!isset($setting['bind'])) {
107
                continue;
108
            }
109
110
            if (!$this->preferences->has($key = 'streams::' . $key)) {
111
                continue;
112
            }
113
114
            $this->config->set($setting['bind'], $this->preferences->value($key));
115
        }
116
    }
117
}
118