AddonManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 230
Duplicated Lines 20.87 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 48
loc 230
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A register() 0 60 3
B getEnabledAddonNamespaces() 24 24 2
B getInstalledAddonNamespaces() 24 24 2
A getAddonNamespace() 0 8 1

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\Streams\Platform\Addon;
2
3
use Anomaly\Streams\Platform\Addon\Event\AddonsHaveRegistered;
4
use Anomaly\Streams\Platform\Addon\Extension\ExtensionModel;
5
use Anomaly\Streams\Platform\Addon\Module\ModuleModel;
6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Contracts\Events\Dispatcher;
8
9
/**
10
 * Class AddonManager
11
 *
12
 * @link    http://pyrocms.com/
13
 * @author  PyroCMS, Inc. <[email protected]>
14
 * @author  Ryan Thompson <[email protected]>
15
 */
16
class AddonManager
17
{
18
19
    /**
20
     * The addon paths.
21
     *
22
     * @var AddonPaths
23
     */
24
    protected $paths;
25
26
    /**
27
     * The addon collection.
28
     *
29
     * @var AddonCollection
30
     */
31
    protected $addons;
32
33
    /**
34
     * The addon loader.
35
     *
36
     * @var AddonLoader
37
     */
38
    protected $loader;
39
40
    /**
41
     * The service container.
42
     *
43
     * @var Container
44
     */
45
    protected $container;
46
47
    /**
48
     * The addon integrator.
49
     *
50
     * @var AddonIntegrator
51
     */
52
    protected $integrator;
53
54
    /**
55
     * The modules model.
56
     *
57
     * @var ModuleModel
58
     */
59
    protected $modules;
60
61
    /**
62
     * The event dispatcher.
63
     *
64
     * @var Dispatcher
65
     */
66
    protected $dispatcher;
67
68
    /**
69
     * The extensions model.
70
     *
71
     * @var ExtensionModel
72
     */
73
    protected $extensions;
74
75
    /**
76
     * Create a new AddonManager instance.
77
     *
78
     * @param AddonPaths      $paths
79
     * @param AddonLoader     $loader
80
     * @param ModuleModel     $modules
81
     * @param Container       $container
82
     * @param Dispatcher      $dispatcher
83
     * @param ExtensionModel  $extensions
84
     * @param AddonIntegrator $integrator
85
     * @param AddonCollection $addons
86
     */
87
    public function __construct(
88
        AddonPaths $paths,
89
        AddonLoader $loader,
90
        ModuleModel $modules,
91
        Container $container,
92
        Dispatcher $dispatcher,
93
        ExtensionModel $extensions,
94
        AddonIntegrator $integrator,
95
        AddonCollection $addons
96
    ) {
97
        $this->paths      = $paths;
98
        $this->addons     = $addons;
99
        $this->loader     = $loader;
100
        $this->modules    = $modules;
101
        $this->container  = $container;
102
        $this->integrator = $integrator;
103
        $this->dispatcher = $dispatcher;
104
        $this->extensions = $extensions;
105
    }
106
107
    /**
108
     * Register all addons.
109
     */
110
    public function register()
111
    {
112
        $enabled   = $this->getEnabledAddonNamespaces();
113
        $installed = $this->getInstalledAddonNamespaces();
114
115
        $this->container->bind(
116
            'streams::addons.enabled',
117
            function () use ($enabled) {
118
                return $enabled;
119
            }
120
        );
121
        $this->container->bind(
122
            'streams::addons.installed',
123
            function () use ($installed) {
124
                return $installed;
125
            }
126
        );
127
128
        $paths = $this->paths->all();
129
130
        /*
131
         * First load all the addons
132
         * so they're available.
133
         */
134
        foreach ($paths as $path) {
135
            $this->loader->load($path);
136
        }
137
138
        $this->loader->register();
139
140
        /*
141
         * Then register all of the addons now
142
         * that they're all PSR autoloaded.
143
         */
144
        foreach ($paths as $path) {
145
146
            $namespace = $this->getAddonNamespace($path);
147
148
            $this->integrator->register(
149
                $path,
150
                $namespace,
151
                in_array($namespace, $enabled),
152
                in_array($namespace, $installed)
153
            );
154
        }
155
156
        // Sort all addons.
157
        $this->addons = $this->addons->sort();
158
159
        /*
160
         * Disperse addons to their
161
         * respective collections and
162
         * finish the integration service.
163
         */
164
        $this->addons->disperse();
165
        $this->addons->registered();
166
        $this->integrator->finish();
167
168
        $this->dispatcher->fire(new AddonsHaveRegistered($this->addons));
169
    }
170
171
    /**
172
     * Get namespaces for enabled addons.
173
     *
174
     * @return array
175
     */
176 View Code Duplication
    protected function getEnabledAddonNamespaces()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
177
    {
178
        if (!env('INSTALLED')) {
179
            return [];
180
        }
181
182
        $modules = $this->modules->cache(
183
            'streams::modules.enabled',
184
            9999,
185
            function () {
186
                return $this->modules->getEnabledNamespaces()->all();
187
            }
188
        );
189
190
        $extensions = $this->extensions->cache(
191
            'streams::extensions.enabled',
192
            9999,
193
            function () {
194
                return $this->extensions->getEnabledNamespaces()->all();
195
            }
196
        );
197
198
        return array_merge($modules, $extensions);
199
    }
200
201
    /**
202
     * Get namespaces for installed addons.
203
     *
204
     * @return array
205
     */
206 View Code Duplication
    protected function getInstalledAddonNamespaces()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
207
    {
208
        if (!env('INSTALLED')) {
209
            return [];
210
        }
211
212
        $modules = $this->modules->cache(
213
            'streams::modules.installed',
214
            9999,
215
            function () {
216
                return $this->modules->getInstalledNamespaces()->all();
217
            }
218
        );
219
220
        $extensions = $this->extensions->cache(
221
            'streams::extensions.installed',
222
            9999,
223
            function () {
224
                return $this->extensions->getInstalledNamespaces()->all();
225
            }
226
        );
227
228
        return array_merge($modules, $extensions);
229
    }
230
231
    /**
232
     * Get the addon namespace.
233
     *
234
     * @param $path
235
     * @return string
236
     */
237
    protected function getAddonNamespace($path)
238
    {
239
        $vendor = strtolower(basename(dirname($path)));
240
        $slug   = strtolower(substr(basename($path), 0, strpos(basename($path), '-')));
241
        $type   = strtolower(substr(basename($path), strpos(basename($path), '-') + 1));
242
243
        return "{$vendor}.{$type}.{$slug}";
244
    }
245
}
246