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

ConfigureSystem   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 101
Duplicated Lines 35.64 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 36
loc 101
wmc 19
lcom 1
cbo 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C handle() 36 58 18

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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