Completed
Push — master ( 80763c...4fecf1 )
by Ryan
15:57
created

StreamsPlugin::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 23
nc 1
nop 11

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php namespace Anomaly\Streams\Platform;
2
3
use Anomaly\Streams\Platform\Addon\AddonCollection;
4
use Anomaly\Streams\Platform\Addon\Plugin\Plugin;
5
use Anomaly\Streams\Platform\Asset\Asset;
6
use Anomaly\Streams\Platform\Entry\Command\GetEntryCriteria;
7
use Anomaly\Streams\Platform\Image\Command\MakeImagePath;
8
use Anomaly\Streams\Platform\Image\Command\MakeImageTag;
9
use Anomaly\Streams\Platform\Image\Command\MakeImageUrl;
10
use Anomaly\Streams\Platform\Image\Image;
11
use Anomaly\Streams\Platform\Support\Decorator;
12
use Anomaly\Streams\Platform\Support\Str;
13
use Anomaly\Streams\Platform\Support\Template;
14
use Anomaly\Streams\Platform\Ui\Button\Command\GetButtons;
15
use Anomaly\Streams\Platform\Ui\Command\GetElapsedTime;
16
use Anomaly\Streams\Platform\Ui\Command\GetMemoryUsage;
17
use Anomaly\Streams\Platform\Ui\Command\GetTranslatedString;
18
use Anomaly\Streams\Platform\Ui\Form\Command\GetFormCriteria;
19
use Anomaly\Streams\Platform\Ui\Icon\Command\GetIcon;
20
use Anomaly\Streams\Platform\View\Command\GetConstants;
21
use Anomaly\Streams\Platform\View\Command\GetLayoutName;
22
use Anomaly\Streams\Platform\View\Command\GetView;
23
use Illuminate\Contracts\Auth\Guard;
24
use Illuminate\Contracts\Config\Repository;
25
use Illuminate\Contracts\Routing\UrlGenerator;
26
use Illuminate\Http\Request;
27
use Illuminate\Routing\Router;
28
use Illuminate\Session\Store;
29
use Illuminate\Translation\Translator;
30
use Jenssegers\Agent\Agent;
31
32
/**
33
 * Class StreamsPlugin
34
 *
35
 * @link          http://anomaly.is/streams-platform
36
 * @author        AnomalyLabs, Inc. <[email protected]>
37
 * @author        Ryan Thompson <[email protected]>
38
 * @package       Anomaly\Streams\Platform
39
 */
40
class StreamsPlugin extends Plugin
41
{
42
43
    /**
44
     * The string utility.
45
     *
46
     * @var Str
47
     */
48
    protected $str;
49
50
    /**
51
     * The URL generator.
52
     *
53
     * @var UrlGenerator
54
     */
55
    protected $url;
56
57
    /**
58
     * The auth guard.
59
     *
60
     * @var Guard
61
     */
62
    protected $auth;
63
64
    /**
65
     * The agent utility.
66
     *
67
     * @var Agent
68
     */
69
    protected $agent;
70
71
    /**
72
     * The asset utility.
73
     *
74
     * @var Asset
75
     */
76
    protected $asset;
77
78
    /**
79
     * The config repository.
80
     *
81
     * @var Repository
82
     */
83
    protected $config;
84
85
    /**
86
     * The image utility.
87
     *
88
     * @var Image
89
     */
90
    protected $image;
91
92
    /**
93
     * The router service.
94
     *
95
     * @var Router
96
     */
97
    protected $router;
98
99
    /**
100
     * The request object.
101
     *
102
     * @var Request
103
     */
104
    protected $request;
105
106
    /**
107
     * The session store.
108
     *
109
     * @var Store
110
     */
111
    protected $session;
112
113
    /**
114
     * The template parser.
115
     *
116
     * @var Template
117
     */
118
    protected $template;
119
120
    /**
121
     * The translator utility.
122
     *
123
     * @var Translator
124
     */
125
    protected $translator;
126
127
    /**
128
     * Create a new AgentPlugin instance.
129
     *
130
     * @param UrlGenerator $url
131
     * @param Str          $str
132
     * @param Guard        $auth
133
     * @param Agent        $agent
134
     * @param Asset        $asset
135
     * @param Image        $image
136
     * @param Router       $router
137
     * @param Repository   $config
138
     * @param Request      $request
139
     * @param Store        $session
140
     * @param Template     $template
141
     */
142
    public function __construct(
143
        UrlGenerator $url,
144
        Str $str,
145
        Guard $auth,
146
        Agent $agent,
147
        Asset $asset,
148
        Image $image,
149
        Router $router,
150
        Repository $config,
151
        Request $request,
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
152
        Store $session,
153
        Template $template
154
    ) {
155
        $this->url      = $url;
156
        $this->str      = $str;
157
        $this->auth     = $auth;
158
        $this->agent    = $agent;
159
        $this->asset    = $asset;
160
        $this->image    = $image;
161
        $this->router   = $router;
162
        $this->config   = $config;
163
        $this->request  = $request;
164
        $this->session  = $session;
165
        $this->template = $template;
166
    }
167
168
    /**
169
     * Get the plugin functions.
170
     *
171
     * @return array
172
     */
173
    public function getFunctions()
174
    {
175
        return [
176
            new \Twig_SimpleFunction(
177
                'entry',
178
                function ($namespace, $stream = null) {
179
                    return (new Decorator())->decorate(
180
                        $this->dispatch(new GetEntryCriteria($namespace, $stream ?: $namespace, 'first'))
181
                    );
182
                }
183
            ),
184
            new \Twig_SimpleFunction(
185
                'entries',
186
                function ($namespace, $stream = null) {
187
                    return (new Decorator())->decorate(
188
                        $this->dispatch(new GetEntryCriteria($namespace, $stream ?: $namespace, 'get'))
189
                    );
190
                }
191
            ),
192
            new \Twig_SimpleFunction(
193
                'image_path',
194
                function ($image) {
195
                    return $this->dispatch(new MakeImagePath($image));
196
                }
197
            ),
198
            new \Twig_SimpleFunction(
199
                'image_url',
200
                function ($image) {
201
                    return $this->dispatch(new MakeImageUrl($image));
202
                }
203
            ),
204
            new \Twig_SimpleFunction(
205
                'image',
206
                function ($image) {
207
                    return $this->dispatch(new MakeImageTag($image));
208
                },
209
                [
210
                    'is_safe' => ['html']
211
                ]
212
            ),
213
            new \Twig_SimpleFunction(
214
                'img',
215
                function ($image) {
216
                    return $this->dispatch(new MakeImageTag($image));
217
                },
218
                [
219
                    'is_safe' => ['html']
220
                ]
221
            ),
222
            new \Twig_SimpleFunction(
223
                'form',
224
                function ($parameters) {
225
                    return $this->dispatch(new GetFormCriteria($parameters));
226
                },
227
                [
228
                    'is_safe' => ['html']
229
                ]
230
            ),
231
            new \Twig_SimpleFunction(
232
                'icon',
233
                function ($type, $class = null) {
234
                    return (new Decorator())->decorate($this->dispatch(new GetIcon($type, $class)));
235
                },
236
                [
237
                    'is_safe' => ['html']
238
                ]
239
            ),
240
            new \Twig_SimpleFunction(
241
                'view',
242
                function ($view, array $data = []) {
243
                    return $this->dispatch(new GetView($view, $data))->render();
244
                },
245
                [
246
                    'is_safe' => ['html']
247
                ]
248
            ),
249
            new \Twig_SimpleFunction(
250
                'buttons',
251
                function ($buttons) {
252
                    return $this->dispatch(new GetButtons($buttons))->render();
253
                },
254
                [
255
                    'is_safe' => ['html']
256
                ]
257
            ),
258
            new \Twig_SimpleFunction(
259
                'constants',
260
                function () {
261
                    return $this->dispatch(new GetConstants())->render();
262
                },
263
                [
264
                    'is_safe' => ['html']
265
                ]
266
            ),
267
            new \Twig_SimpleFunction(
268
                'env',
269
                function ($key, $default = null) {
270
                    return env($key, $default);
271
                }
272
            ),
273
            new \Twig_SimpleFunction(
274
                'decorate',
275
                function ($value) {
276
                    return (new Decorator())->decorate($value);
277
                }
278
            ),
279
            new \Twig_SimpleFunction(
280
                'request_time',
281
                function ($decimal = 2) {
282
                    return $this->dispatch(new GetElapsedTime($decimal));
283
                }
284
            ),
285
            new \Twig_SimpleFunction(
286
                'memory_usage',
287
                function ($precision = 1) {
288
                    return $this->dispatch(new GetMemoryUsage($precision));
289
                }
290
            ),
291
            new \Twig_SimpleFunction(
292
                'layout',
293
                function ($layout, $default = 'default') {
294
                    return $this->dispatch(new GetLayoutName($layout, $default));
295
                }
296
            ),
297
            new \Twig_SimpleFunction(
298
                'request_*',
299 View Code Duplication
                function ($name) {
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...
300
301
                    $arguments = array_slice(func_get_args(), 1);
302
303
                    return call_user_func_array([$this->request, camel_case($name)], $arguments);
304
                }
305
            ),
306
            new \Twig_SimpleFunction(
307
                'trans',
308
                function ($key, array $parameters = [], $locale = 'en') {
309
                    return $this->dispatch(new GetTranslatedString($key, $parameters, $locale));
310
                }
311
            ),
312
            new \Twig_SimpleFunction(
313
                'str_*',
314 View Code Duplication
                function ($name) {
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...
315
316
                    $arguments = array_slice(func_get_args(), 1);
317
318
                    return call_user_func_array([$this->str, camel_case($name)], $arguments);
319
                }
320
            ),
321
            new \Twig_SimpleFunction(
322
                'url_*',
323 View Code Duplication
                function ($name) {
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...
324
325
                    $arguments = array_slice(func_get_args(), 1);
326
327
                    return call_user_func_array([$this->url, camel_case($name)], $arguments);
328
                }
329
            ),
330
            new \Twig_SimpleFunction(
331
                'addons',
332
                function ($type = null) {
333
334
                    $addons = app(AddonCollection::class);
335
336
                    if ($type) {
337
                        $addons = $addons->{str_plural($type)}();
338
                    }
339
340
                    return $addons;
341
                }
342
            ),
343
            new \Twig_SimpleFunction('input_get', [$this->request, 'input']),
344
            new \Twig_SimpleFunction('asset', [$this->url, 'asset'], ['is_safe' => ['html']]),
345
            new \Twig_SimpleFunction('action', [$this->url, 'action'], ['is_safe' => ['html']]),
346
            new \Twig_SimpleFunction('url', [$this, 'url'], ['is_safe' => ['html']]),
347
            new \Twig_SimpleFunction('route', [$this->url, 'route'], ['is_safe' => ['html']]),
348
            new \Twig_SimpleFunction('route_has', [$this->router, 'has'], ['is_safe' => ['html']]),
349
            new \Twig_SimpleFunction('secure_url', [$this->url, 'secure'], ['is_safe' => ['html']]),
350
            new \Twig_SimpleFunction('secure_asset', [$this->url, 'secureAsset'], ['is_safe' => ['html']]),
351
            new \Twig_SimpleFunction('config', [$this->config, 'get']),
352
            new \Twig_SimpleFunction('config_get', [$this->config, 'get']),
353
            new \Twig_SimpleFunction('config_has', [$this->config, 'has']),
354
            new \Twig_SimpleFunction('auth_user', [$this->auth, 'user']),
355
            new \Twig_SimpleFunction('auth_check', [$this->auth, 'check']),
356
            new \Twig_SimpleFunction('auth_guest', [$this->auth, 'guest']),
357
            new \Twig_SimpleFunction('trans_exists', [$this->translator, 'exists']),
358
            new \Twig_SimpleFunction('message_get', [$this->session, 'pull']),
359
            new \Twig_SimpleFunction('message_exists', [$this->session, 'has']),
360
            new \Twig_SimpleFunction('session', [$this->session, 'get']),
361
            new \Twig_SimpleFunction('parse', [$this->template, 'render'], ['is_safe' => ['html']]),
362
            new \Twig_SimpleFunction('csrf_token', [$this->session, 'token'], ['is_safe' => ['html']]),
363
            new \Twig_SimpleFunction('csrf_field', 'csrf_field', ['is_safe' => ['html']]),
364
            new \Twig_SimpleFunction('session_get', [$this->session, 'get']),
365
            new \Twig_SimpleFunction('session_pull', [$this->session, 'pull']),
366
            new \Twig_SimpleFunction('session_has', [$this->session, 'has']),
367
            new \Twig_SimpleFunction('agent_device', [$this->agent, 'device']),
368
            new \Twig_SimpleFunction('agent_browser', [$this->agent, 'browser']),
369
            new \Twig_SimpleFunction('agent_platform', [$this->agent, 'platform']),
370
            new \Twig_SimpleFunction('agent_is_phone', [$this->agent, 'isPhone']),
371
            new \Twig_SimpleFunction('agent_is_robot', [$this->agent, 'isRobot']),
372
            new \Twig_SimpleFunction('agent_is_tablet', [$this->agent, 'isTablet']),
373
            new \Twig_SimpleFunction('agent_is_mobile', [$this->agent, 'isMobile']),
374
            new \Twig_SimpleFunction('agent_is_desktop', [$this->agent, 'isDesktop']),
375
            new \Twig_SimpleFunction('asset_add', [$this->asset, 'add']),
376
            new \Twig_SimpleFunction('asset_url', [$this->asset, 'url']),
377
            new \Twig_SimpleFunction('asset_urls', [$this->asset, 'urls']),
378
            new \Twig_SimpleFunction('asset_path', [$this->asset, 'path']),
379
            new \Twig_SimpleFunction('asset_paths', [$this->asset, 'paths']),
380
            new \Twig_SimpleFunction('asset_download', [$this->asset, 'download']),
381
            new \Twig_SimpleFunction('asset_style', [$this->asset, 'style'], ['is_safe' => ['html']]),
382
            new \Twig_SimpleFunction('asset_styles', [$this->asset, 'styles'], ['is_safe' => ['html']]),
383
            new \Twig_SimpleFunction('asset_inline', [$this->asset, 'inline'], ['is_safe' => ['html']]),
384
            new \Twig_SimpleFunction('asset_script', [$this->asset, 'script'], ['is_safe' => ['html']]),
385
            new \Twig_SimpleFunction('asset_scripts', [$this->asset, 'scripts'], ['is_safe' => ['html']])
386
        ];
387
    }
388
389
    /**
390
     * Get the filters.
391
     *
392
     * @return array
393
     */
394
    public function getFilters()
395
    {
396
        return [
397
            new \Twig_SimpleFilter('camel_case', [$this->str, 'camel']),
398
            new \Twig_SimpleFilter('snake_case', [$this->str, 'snake']),
399
            new \Twig_SimpleFilter('studly_case', [$this->str, 'studly']),
400
            new \Twig_SimpleFilter('humanize', [$this->str, 'humanize']),
401
            new \Twig_SimpleFilter(
402
                'str_*',
403 View Code Duplication
                function ($name) {
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...
404
405
                    $arguments = array_slice(func_get_args(), 1);
406
407
                    return call_user_func_array([$this->str, camel_case($name)], $arguments);
408
                }
409
            ),
410
        ];
411
    }
412
413
    /**
414
     * Return a URL.
415
     *
416
     * @param null  $path
417
     * @param array $parameters
418
     * @param null  $secure
419
     * @return string
420
     */
421
    public function url($path = null, $parameters = [], $secure = null)
422
    {
423
        return $this->url->to($path, $parameters, $secure);
424
    }
425
}
426