AddonCollection::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php namespace Anomaly\Streams\Platform\Addon;
2
3
use Anomaly\Streams\Platform\Addon\Extension\Extension;
4
use Anomaly\Streams\Platform\Addon\Module\Module;
5
use Illuminate\Support\Collection;
6
7
class AddonCollection extends Collection
8
{
9
10
    /**
11
     * Create a new AddonCollection instance.
12
     *
13
     * @param array $items
14
     */
15
    public function __construct($items = [])
16
    {
17
        /* @var Addon $item */
18
        foreach ($items as $key => $item) {
19
            if ($item instanceof Addon) {
20
                $key = $item->getNamespace();
21
            }
22
23
            $this->items[$key] = $item;
24
        }
25
    }
26
27
    /**
28
     * Return all addon namespaces.
29
     *
30
     * @param  null $key
31
     * @return array
32
     */
33
    public function namespaces($key = null)
34
    {
35
        return array_values(
36
            $this->map(
37
                function (Addon $addon) use ($key) {
38
                    return $addon->getNamespace($key);
39
                }
40
            )->all()
41
        );
42
    }
43
44
    /**
45
     * Return only core addons.
46
     *
47
     * @return AddonCollection
48
     */
49
    public function core()
50
    {
51
        $core = [];
52
53
        /* @var Addon $item */
54
        foreach ($this->items as $item) {
55
            if ($item->isCore()) {
56
                $core[] = $item;
57
            }
58
        }
59
60
        return self::make($core);
61
    }
62
63
    /**
64
     * Return only testing addons.
65
     *
66
     * @return AddonCollection
67
     */
68
    public function testing()
69
    {
70
        $testing = [];
71
72
        /* @var Addon $item */
73
        foreach ($this->items as $item) {
74
            if ($item->isTesting()) {
75
                $testing[] = $item;
76
            }
77
        }
78
79
        return self::make($testing);
80
    }
81
82
    /**
83
     * Get an addon.
84
     *
85
     * @param  mixed $key
86
     * @param  null  $default
87
     * @return Addon|mixed|null
88
     */
89
    public function get($key, $default = null)
90
    {
91
        if (!$addon = parent::get($key, $default)) {
92
            return $this->findBySlug($key);
93
        }
94
95
        return $addon;
96
    }
97
98
    /**
99
     * Find an addon by it's slug.
100
     *
101
     * @param  $slug
102
     *
103
     * @return null|Addon
104
     */
105
    public function findBySlug($slug)
106
    {
107
        /* @var Addon $item */
108
        foreach ($this->items as $item) {
109
            if ($item->getSlug() == $slug) {
110
                return $item;
111
            }
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * Order addon's by their slug.
119
     *
120
     * @param string $direction
121
     *
122
     * @return AddonCollection
123
     */
124
    public function orderBySlug($direction = 'asc')
125
    {
126
        $ordered = [];
127
128
        /* @var Addon $item */
129
        foreach ($this->items as $item) {
130
            $ordered[$item->getNamespace()] = $item;
131
        }
132
133
        if ($direction == 'asc') {
134
            ksort($ordered);
135
        } else {
136
            krsort($ordered);
137
        }
138
139
        return self::make($ordered);
140
    }
141
142
    /**
143
     * Disperse addons to their
144
     * respective collections.
145
     */
146
    public function disperse()
147
    {
148
        foreach (config('streams::addons.types') as $type) {
149
150
            /* @var AddonCollection $collection */
151
            $collection = app("{$type}.collection");
152
153
            /* @var Addon $addon */
154
            foreach ($this->items as $addon) {
155
                if ($addon->getType() !== $type) {
156
                    continue;
157
                }
158
159
                $collection->put($addon->getNamespace(), $addon);
160
            }
161
        }
162
    }
163
164
    /**
165
     * Fire the registered
166
     * method on all addons.
167
     */
168
    public function registered()
169
    {
170
        $this->map(
171
            function (Addon $addon) {
172
                $addon->fire('registered');
173
            }
174
        );
175
    }
176
177
    /**
178
     * Return addons only with the provided configuration.
179
     *
180
     * @param $key
181
     * @return AddonCollection
182
     */
183
    public function withConfig($key)
184
    {
185
        $addons = [];
186
187
        /* @var Addon $item */
188
        foreach ($this->items as $item) {
189
            if ($item->hasConfig($key)) {
190
                $addons[] = $item;
191
            }
192
        }
193
194
        return self::make($addons);
195
    }
196
197
    /**
198
     * Return addons only with any of
199
     * the provided configuration.
200
     *
201
     * @param  array $keys
202
     * @return AddonCollection
203
     */
204
    public function withAnyConfig(array $keys)
205
    {
206
        $addons = [];
207
208
        /* @var Addon $item */
209
        foreach ($this->items as $item) {
210
            if ($item->hasAnyConfig($keys)) {
211
                $addons[] = $item;
212
            }
213
        }
214
215
        return self::make($addons);
216
    }
217
218
    /**
219
     * Sort through each item with a callback.
220
     *
221
     * @param  callable|null $callback
222
     * @return static
223
     */
224
    public function sort(callable $callback = null)
225
    {
226
        return parent::sort(
227
            $callback ?: function (Addon $a, Addon $b) {
228
                if ($a->getSlug() == $b->getSlug()) {
229
                    return 0;
230
                }
231
232
                return ($a->getSlug() < $b->getSlug()) ? -1 : 1;
233
            }
234
        );
235
    }
236
237
    /**
238
     * Return only installable addons.
239
     *
240
     * @return AddonCollection
241
     */
242
    public function installable()
243
    {
244
        return $this->filter(
245
            function (Addon $addon) {
246
                return in_array($addon->getType(), ['module', 'extension']);
247
            }
248
        );
249
    }
250
251
    /**
252
     * Return enabled addons.
253
     *
254
     * @return AddonCollection
255
     */
256
    public function enabled()
257
    {
258
        return $this->installable()->filter(
259
            function ($addon) {
260
261
                /* @var Module|Extension $addon */
262
                return $addon->isEnabled();
263
            }
264
        );
265
    }
266
267
    /**
268
     * Return installed addons.
269
     *
270
     * @return AddonCollection
271
     */
272
    public function installed()
273
    {
274
        return $this->installable()->filter(
275
            function ($addon) {
276
277
                /* @var Module|Extension $addon */
278
                return $addon->isInstalled();
279
            }
280
        );
281
    }
282
283
    /**
284
     * Return uninstalled addons.
285
     *
286
     * @return AddonCollection
287
     */
288
    public function uninstalled()
289
    {
290
        return $this->installable()->filter(
291
            function ($addon) {
292
293
                /* @var Module|Extension $addon */
294
                return !$addon->isInstalled();
295
            }
296
        );
297
    }
298
299
    /**
300
     * Call a method.
301
     *
302
     * @param $method
303
     * @param $arguments
304
     * @return AddonCollection
305
     */
306 View Code Duplication
    public function __call($method, $arguments)
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...
307
    {
308
        $type = str_singular($method);
309
310
        if (in_array($type, config('streams::addons.types'))) {
311
            return app("{$type}.collection");
312
        }
313
314
        return call_user_func_array([$this, $method], $arguments);
315
    }
316
317
    /**
318
     * Get a property.
319
     *
320
     * @param $name
321
     * @return AddonCollection
322
     */
323 View Code Duplication
    public function __get($name)
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...
324
    {
325
        $type = str_singular($name);
326
327
        if (in_array($type, config('streams::addons.types'))) {
328
            return app("{$type}.collection");
329
        }
330
331
        return $this->{$name};
332
    }
333
}
334