StreamsPlugin   C
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 506
Duplicated Lines 7.91 %

Coupling/Cohesion

Components 1
Dependencies 26

Importance

Changes 0
Metric Value
dl 40
loc 506
rs 5
c 0
b 0
f 0
wmc 11
lcom 1
cbo 26

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 31 1
C getFunctions() 34 308 8
A getFilters() 6 17 1
A url() 0 4 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;
2
3
use Anomaly\Streams\Platform\Addon\AddonCollection;
4
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter;
5
use Anomaly\Streams\Platform\Addon\Plugin\Plugin;
6
use Anomaly\Streams\Platform\Asset\Asset;
7
use Anomaly\Streams\Platform\Entry\Command\GetEntryCriteria;
8
use Anomaly\Streams\Platform\Image\Command\MakeImagePath;
9
use Anomaly\Streams\Platform\Image\Command\MakeImageTag;
10
use Anomaly\Streams\Platform\Image\Command\MakeImageUrl;
11
use Anomaly\Streams\Platform\Image\Image;
12
use Anomaly\Streams\Platform\Model\Command\GetEloquentCriteria;
13
use Anomaly\Streams\Platform\Routing\UrlGenerator;
14
use Anomaly\Streams\Platform\Stream\Command\GetStream;
15
use Anomaly\Streams\Platform\Stream\Command\GetStreams;
16
use Anomaly\Streams\Platform\Support\Currency;
17
use Anomaly\Streams\Platform\Support\Decorator;
18
use Anomaly\Streams\Platform\Support\Str;
19
use Anomaly\Streams\Platform\Support\Template;
20
use Anomaly\Streams\Platform\Ui\Breadcrumb\BreadcrumbCollection;
21
use Anomaly\Streams\Platform\Ui\Button\Command\GetButtons;
22
use Anomaly\Streams\Platform\Ui\Command\GetElapsedTime;
23
use Anomaly\Streams\Platform\Ui\Command\GetMemoryUsage;
24
use Anomaly\Streams\Platform\Ui\Command\GetTranslatedString;
25
use Anomaly\Streams\Platform\Ui\Form\Command\GetFormCriteria;
26
use Anomaly\Streams\Platform\Ui\Icon\Command\GetIcon;
27
use Anomaly\Streams\Platform\View\Command\GetConstants;
28
use Anomaly\Streams\Platform\View\Command\GetLayoutName;
29
use Anomaly\Streams\Platform\View\Command\GetView;
30
use Carbon\Carbon;
31
use Illuminate\Contracts\Auth\Guard;
32
use Illuminate\Contracts\Config\Repository;
33
use Illuminate\Http\Request;
34
use Illuminate\Routing\Route;
35
use Illuminate\Routing\Router;
36
use Illuminate\Session\Store;
37
use Illuminate\Translation\Translator;
38
use Jenssegers\Agent\Agent;
39
use Symfony\Component\Yaml\Yaml;
40
41
/**
42
 * Class StreamsPlugin
43
 *
44
 * @link   http://pyrocms.com/
45
 * @author PyroCMS, Inc. <[email protected]>
46
 * @author Ryan Thompson <[email protected]>
47
 */
48
class StreamsPlugin extends Plugin
49
{
50
51
    /**
52
     * The string utility.
53
     *
54
     * @var Str
55
     */
56
    protected $str;
57
58
    /**
59
     * The URL generator.
60
     *
61
     * @var UrlGenerator
62
     */
63
    protected $url;
64
65
    /**
66
     * The auth guard.
67
     *
68
     * @var Guard
69
     */
70
    protected $auth;
71
72
    /**
73
     * The YAML parser.
74
     *
75
     * @var Yaml
76
     */
77
    protected $yaml;
78
79
    /**
80
     * The agent utility.
81
     *
82
     * @var Agent
83
     */
84
    protected $agent;
85
86
    /**
87
     * The asset utility.
88
     *
89
     * @var Asset
90
     */
91
    protected $asset;
92
93
    /**
94
     * The config repository.
95
     *
96
     * @var Repository
97
     */
98
    protected $config;
99
100
    /**
101
     * The image utility.
102
     *
103
     * @var Image
104
     */
105
    protected $image;
106
107
    /**
108
     * The active route.
109
     *
110
     * @var Route
111
     */
112
    protected $route;
113
114
    /**
115
     * The router service.
116
     *
117
     * @var Router
118
     */
119
    protected $router;
120
121
    /**
122
     * The request object.
123
     *
124
     * @var Request
125
     */
126
    protected $request;
127
128
    /**
129
     * The session store.
130
     *
131
     * @var Store
132
     */
133
    protected $session;
134
135
    /**
136
     * The currency utility.
137
     *
138
     * @var Currency
139
     */
140
    protected $currency;
141
142
    /**
143
     * The template parser.
144
     *
145
     * @var Template
146
     */
147
    protected $template;
148
149
    /**
150
     * The translator utility.
151
     *
152
     * @var Translator
153
     */
154
    protected $translator;
155
156
    /**
157
     * Create a new AgentPlugin instance.
158
     *
159
     * @param UrlGenerator $url
160
     * @param Str          $str
161
     * @param Guard        $auth
162
     * @param Agent        $agent
163
     * @param Asset        $asset
164
     * @param Image        $image
165
     * @param Router       $router
166
     * @param Repository   $config
167
     * @param Request      $request
168
     * @param Store        $session
169
     * @param Currency     $currency
170
     * @param Template     $template
171
     */
172
    public function __construct(
173
        UrlGenerator $url,
174
        Str $str,
175
        Guard $auth,
176
        Yaml $yaml,
177
        Agent $agent,
178
        Asset $asset,
179
        Image $image,
180
        Router $router,
181
        Repository $config,
182
        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...
183
        Store $session,
184
        Currency $currency,
185
        Template $template
186
    ) {
187
        $this->url      = $url;
188
        $this->str      = $str;
189
        $this->auth     = $auth;
190
        $this->yaml     = $yaml;
191
        $this->agent    = $agent;
192
        $this->asset    = $asset;
193
        $this->image    = $image;
194
        $this->router   = $router;
195
        $this->config   = $config;
196
        $this->request  = $request;
197
        $this->session  = $session;
198
        $this->currency = $currency;
199
        $this->template = $template;
200
201
        $this->route = $request->route();
0 ignored issues
show
Documentation Bug introduced by
It seems like $request->route() can also be of type string. However, the property $route is declared as type object<Illuminate\Routing\Route>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
202
    }
203
204
    /**
205
     * Get the plugin functions.
206
     *
207
     * @return array
208
     */
209
    public function getFunctions()
0 ignored issues
show
Coding Style introduced by
getFunctions uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
210
    {
211
        return [
212
            new \Twig_SimpleFunction(
213
                'stream',
214
                function ($namespace, $slug = null) {
215
                    return (new Decorator())->decorate(
216
                        $this->dispatch(new GetStream($namespace, $slug ?: $namespace))
217
                    );
218
                }
219
            ),
220
            new \Twig_SimpleFunction(
221
                'streams',
222
                function ($namespace) {
223
                    return (new Decorator())->decorate(
224
                        $this->dispatch(new GetStreams($namespace))
225
                    );
226
                }
227
            ),
228
            new \Twig_SimpleFunction(
229
                'entry',
230
                function ($namespace, $stream = null) {
231
                    return (new Decorator())->decorate(
232
                        $this->dispatch(new GetEntryCriteria($namespace, $stream ?: $namespace, 'first'))
233
                    );
234
                }
235
            ),
236
            new \Twig_SimpleFunction(
237
                'entries',
238
                function ($namespace, $stream = null) {
239
                    return (new Decorator())->decorate(
240
                        $this->dispatch(new GetEntryCriteria($namespace, $stream ?: $namespace, 'get'))
241
                    );
242
                }
243
            ),
244
            new \Twig_SimpleFunction(
245
                'query',
246
                function ($model = null) {
247
                    return (new Decorator())->decorate(
248
                        $this->dispatch(new GetEloquentCriteria($model, 'get'))
249
                    );
250
                }
251
            ),
252
            new \Twig_SimpleFunction(
253
                'image_path',
254
                function ($image) {
255
                    return $this->dispatch(new MakeImagePath($image));
256
                }
257
            ),
258
            new \Twig_SimpleFunction(
259
                'image_url',
260
                function ($image) {
261
                    return $this->dispatch(new MakeImageUrl($image));
262
                }
263
            ),
264
            new \Twig_SimpleFunction(
265
                'image',
266
                function ($image) {
267
                    return $this->dispatch(new MakeImageTag($image));
268
                },
269
                [
270
                    'is_safe' => ['html'],
271
                ]
272
            ),
273
            new \Twig_SimpleFunction(
274
                'img',
275
                function ($image) {
276
                    return $this->dispatch(new MakeImageTag($image));
277
                },
278
                [
279
                    'is_safe' => ['html'],
280
                ]
281
            ),
282
            new \Twig_SimpleFunction(
283
                'form',
284
                function () {
285
                    $arguments = func_get_args();
286
287
                    if (count($arguments) >= 2) {
288
                        $arguments = [
289
                            'namespace' => array_get(func_get_args(), 0),
290
                            'stream'    => array_get(func_get_args(), 1),
291
                            'entry'     => array_get(func_get_args(), 2),
292
                        ];
293
                    }
294
295
                    if (count($arguments) == 1) {
296
                        $arguments = func_get_arg(0);
297
                    }
298
299
                    return $this->dispatch(new GetFormCriteria($arguments));
300
                },
301
                [
302
                    'is_safe' => ['html'],
303
                ]
304
            ),
305
            new \Twig_SimpleFunction(
306
                'icon',
307
                function ($type, $class = null) {
308
                    return (new Decorator())->decorate($this->dispatch(new GetIcon($type, $class)));
309
                },
310
                [
311
                    'is_safe' => ['html'],
312
                ]
313
            ),
314
            new \Twig_SimpleFunction(
315
                'view',
316
                function ($view, array $data = []) {
317
                    return $this->dispatch(new GetView($view, $data))->render();
318
                },
319
                [
320
                    'is_safe' => ['html'],
321
                ]
322
            ),
323
            new \Twig_SimpleFunction(
324
                'buttons',
325
                function ($buttons) {
326
                    return $this->dispatch(new GetButtons($buttons))->render();
327
                },
328
                [
329
                    'is_safe' => ['html'],
330
                ]
331
            ),
332
            new \Twig_SimpleFunction(
333
                'constants',
334
                function () {
335
                    return $this->dispatch(new GetConstants())->render();
336
                },
337
                [
338
                    'is_safe' => ['html'],
339
                ]
340
            ),
341
            new \Twig_SimpleFunction(
342
                'env',
343
                function ($key, $default = null) {
344
                    return env($key, $default);
345
                }
346
            ),
347
            new \Twig_SimpleFunction(
348
                'carbon',
349
                function ($time = null, $timezone = null) {
350
                    return new Carbon($time, $timezone);
351
                }
352
            ),
353
            new \Twig_SimpleFunction(
354
                'decorate',
355
                function ($value) {
356
                    return (new Decorator())->decorate($value);
357
                }
358
            ),
359
            new \Twig_SimpleFunction(
360
                'request_time',
361
                function ($decimal = 2) {
362
                    return $this->dispatch(new GetElapsedTime($decimal));
363
                }
364
            ),
365
            new \Twig_SimpleFunction(
366
                'memory_usage',
367
                function ($precision = 1) {
368
                    return $this->dispatch(new GetMemoryUsage($precision));
369
                }
370
            ),
371
            new \Twig_SimpleFunction(
372
                'layout',
373
                function ($layout, $default = 'default') {
374
                    return $this->dispatch(new GetLayoutName($layout, $default));
375
                }
376
            ),
377
            new \Twig_SimpleFunction(
378
                'request_*',
379 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...
380
                    $arguments = array_slice(func_get_args(), 1);
381
382
                    return call_user_func_array([$this->request, camel_case($name)], $arguments);
383
                }
384
            ),
385
            new \Twig_SimpleFunction(
386
                'trans',
387
                function ($key, array $parameters = [], $locale = 'en') {
388
                    return $this->dispatch(new GetTranslatedString($key, $parameters, $locale));
389
                }
390
            ),
391
            new \Twig_SimpleFunction(
392
                'str_*',
393 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...
394
                    $arguments = array_slice(func_get_args(), 1);
395
396
                    return call_user_func_array([$this->str, camel_case($name)], $arguments);
397
                }
398
            ),
399
            new \Twig_SimpleFunction(
400
                'url_*',
401 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...
402
                    $arguments = array_slice(func_get_args(), 1);
403
404
                    return call_user_func_array([$this->url, camel_case($name)], $arguments);
405
                }
406
            ),
407
            new \Twig_SimpleFunction(
408
                'route_*',
409 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...
410
                    $arguments = array_slice(func_get_args(), 1);
411
412
                    return call_user_func_array([$this->route, camel_case($name)], $arguments);
413
                }
414
            ),
415
            new \Twig_SimpleFunction(
416
                'asset_*',
417 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...
418
                    $arguments = array_slice(func_get_args(), 1);
419
420
                    return call_user_func_array([$this->asset, camel_case($name)], $arguments);
421
                }, ['is_safe' => ['html']]
422
            ),
423
            new \Twig_SimpleFunction(
424
                'currency_*',
425 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...
426
                    $arguments = array_slice(func_get_args(), 1);
427
428
                    return call_user_func_array([$this->currency, camel_case($name)], $arguments);
429
                }
430
            ),
431
            new \Twig_SimpleFunction(
432
                'yaml',
433
                function ($input) {
434
435
                    if ($input instanceof FieldTypePresenter) {
436
                        $input = $input->__toString();
437
                    }
438
439
                    return $this->yaml->parse($input);
440
                }
441
            ),
442
            new \Twig_SimpleFunction(
443
                'addon',
444
                function ($identifier) {
445
                    return (new Decorator())->decorate(app(AddonCollection::class)->get($identifier));
446
                }
447
            ),
448
            new \Twig_SimpleFunction(
449
                'addons',
450
                function ($type = null) {
451
                    $addons = app(AddonCollection::class);
452
453
                    if ($type) {
454
                        $addons = $addons->{str_plural($type)}();
455
                    }
456
457
                    return (new Decorator())->decorate($addons);
458
                }
459
            ),
460
            new \Twig_SimpleFunction(
461
                'breadcrumb',
462
                function () {
463
                    return app(BreadcrumbCollection::class);
464
                }, ['is_safe' => ['html']]
465
            ),
466
            new \Twig_SimpleFunction(
467
                'gravatar',
468
                function ($email, array $parameters = []) {
469
                    return $this->image->make(
470
                        'https://www.gravatar.com/avatar/' . md5($email) . '?' . http_build_query(
471
                            $parameters
472
                        ),
473
                        'image'
474
                    );
475
                }, ['is_safe' => ['html']]
476
            ),
477
            new \Twig_SimpleFunction(
478
                'cookie',
479
                function ($key, $default = null) {
480
                    return array_get($_COOKIE, $key, $default);
481
                }
482
            ),
483
            new \Twig_SimpleFunction('input_get', [$this->request, 'input']),
484
            new \Twig_SimpleFunction('asset', [$this->url, 'asset'], ['is_safe' => ['html']]),
485
            new \Twig_SimpleFunction('action', [$this->url, 'action'], ['is_safe' => ['html']]),
486
            new \Twig_SimpleFunction('url', [$this, 'url'], ['is_safe' => ['html']]),
487
            new \Twig_SimpleFunction('route', [$this->url, 'route'], ['is_safe' => ['html']]),
488
            new \Twig_SimpleFunction('route_has', [$this->router, 'has'], ['is_safe' => ['html']]),
489
            new \Twig_SimpleFunction('secure_url', [$this->url, 'secure'], ['is_safe' => ['html']]),
490
            new \Twig_SimpleFunction('secure_asset', [$this->url, 'secureAsset'], ['is_safe' => ['html']]),
491
            new \Twig_SimpleFunction('config', [$this->config, 'get']),
492
            new \Twig_SimpleFunction('config_get', [$this->config, 'get']),
493
            new \Twig_SimpleFunction('config_has', [$this->config, 'has']),
494
            new \Twig_SimpleFunction('auth_user', [$this->auth, 'user']),
495
            new \Twig_SimpleFunction('auth_check', [$this->auth, 'check']),
496
            new \Twig_SimpleFunction('auth_guest', [$this->auth, 'guest']),
497
            new \Twig_SimpleFunction('trans_exists', [$this->translator, 'exists']),
498
            new \Twig_SimpleFunction('message_get', [$this->session, 'pull']),
499
            new \Twig_SimpleFunction('message_exists', [$this->session, 'has']),
500
            new \Twig_SimpleFunction('session', [$this->session, 'get']),
501
            new \Twig_SimpleFunction('parse', [$this->template, 'render'], ['is_safe' => ['html']]),
502
            new \Twig_SimpleFunction('csrf_token', [$this->session, 'token'], ['is_safe' => ['html']]),
503
            new \Twig_SimpleFunction('csrf_field', 'csrf_field', ['is_safe' => ['html']]),
504
            new \Twig_SimpleFunction('session_get', [$this->session, 'get']),
505
            new \Twig_SimpleFunction('session_pull', [$this->session, 'pull']),
506
            new \Twig_SimpleFunction('session_has', [$this->session, 'has']),
507
            new \Twig_SimpleFunction('agent_device', [$this->agent, 'device']),
508
            new \Twig_SimpleFunction('agent_browser', [$this->agent, 'browser']),
509
            new \Twig_SimpleFunction('agent_platform', [$this->agent, 'platform']),
510
            new \Twig_SimpleFunction('agent_is_phone', [$this->agent, 'isPhone']),
511
            new \Twig_SimpleFunction('agent_is_robot', [$this->agent, 'isRobot']),
512
            new \Twig_SimpleFunction('agent_is_tablet', [$this->agent, 'isTablet']),
513
            new \Twig_SimpleFunction('agent_is_mobile', [$this->agent, 'isMobile']),
514
            new \Twig_SimpleFunction('agent_is_desktop', [$this->agent, 'isDesktop']),
515
        ];
516
    }
517
518
    /**
519
     * Get the filters.
520
     *
521
     * @return array
522
     */
523
    public function getFilters()
524
    {
525
        return [
526
            new \Twig_SimpleFilter('camel_case', [$this->str, 'camel']),
527
            new \Twig_SimpleFilter('snake_case', [$this->str, 'snake']),
528
            new \Twig_SimpleFilter('studly_case', [$this->str, 'studly']),
529
            new \Twig_SimpleFilter('humanize', [$this->str, 'humanize']),
530
            new \Twig_SimpleFilter(
531
                'str_*',
532 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...
533
                    $arguments = array_slice(func_get_args(), 1);
534
535
                    return call_user_func_array([$this->str, camel_case($name)], $arguments);
536
                }
537
            ),
538
        ];
539
    }
540
541
    /**
542
     * Return a URL.
543
     *
544
     * @param  null  $path
545
     * @param  array $parameters
546
     * @param  null  $secure
547
     * @return string
548
     */
549
    public function url($path = null, $parameters = [], $secure = null)
550
    {
551
        return $this->url->to($path, $parameters, $secure);
552
    }
553
}
554