Completed
Push — master ( 2242cd...489a99 )
by Ryan
07:43
created

HrefGuesser::guess()   D

Complexity

Conditions 10
Paths 14

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 4.8196
cc 10
eloc 25
nc 14
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Anomaly\Streams\Platform\Ui\ControlPanel\Component\Button\Guesser;
2
3
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection;
4
use Anomaly\Streams\Platform\Ui\ControlPanel\ControlPanelBuilder;
5
use Illuminate\Contracts\Routing\UrlGenerator;
6
use Illuminate\Http\Request;
7
8
/**
9
 * Class HrefGuesser
10
 *
11
 * @link          http://anomaly.is/streams-Platform
12
 * @author        AnomalyLabs, Inc. <[email protected]>
13
 * @author        Ryan Thompson <[email protected]>
14
 * @package       Anomaly\Streams\Platform\Ui\ControlPanel\Component\Button\Guesser
15
 */
16
class HrefGuesser
17
{
18
19
    /**
20
     * The URL generator.
21
     *
22
     * @var UrlGenerator
23
     */
24
    protected $url;
25
26
    /**
27
     * The request object.
28
     *
29
     * @var Request
30
     */
31
    protected $request;
32
33
    /**
34
     * The module collection.
35
     *
36
     * @var ModuleCollection
37
     */
38
    protected $modules;
39
40
    /**
41
     * Create a new HrefGuesser instance.
42
     *
43
     * @param UrlGenerator     $url
44
     * @param Request          $request
45
     * @param ModuleCollection $modules
46
     */
47
    public function __construct(UrlGenerator $url, Request $request, ModuleCollection $modules)
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...
48
    {
49
        $this->url     = $url;
50
        $this->request = $request;
51
        $this->modules = $modules;
52
    }
53
54
    /**
55
     * Guess the HREF for a button.
56
     *
57
     * @param ControlPanelBuilder $builder
58
     */
59
    public function guess(ControlPanelBuilder $builder)
60
    {
61
        $buttons  = $builder->getButtons();
62
        $sections = $builder->getControlPanelSections();
63
64
        $active = $sections->active();
65
        $module = $this->modules->active();
66
67
        foreach ($buttons as &$button) {
68
69
            // If we already have an HREF then skip it.
70
            if (isset($button['attributes']['href'])) {
71
                continue;
72
            }
73
74
            // Determine the HREF based on the button type.
75
            switch (array_get($button, 'button')) {
76
77
                case 'add':
78
                case 'new':
79
                case 'create':
80
                    $button['attributes']['href'] = $active->getHref('create');
81
                    break;
82
83
                case 'export':
84
                    if ($module) {
85
                        $button['attributes']['href'] = $this->url->to(
86
                            'entry/handle/export/' . $module->getNamespace() . '/' . array_get(
87
                                $button,
88
                                'namespace'
89
                            ) . '/' . array_get($button, 'stream')
90
                        );
91
                    }
92
                    break;
93
            }
94
95
            if (!isset($button['attributes']['href']) && isset($button['button'])) {
96
                $button['attributes']['href'] = $active->getHref($button['button']);
97
            }
98
        }
99
100
        $builder->setButtons($buttons);
101
    }
102
}
103