SetActiveSection   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 120
Duplicated Lines 2.5 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 3
loc 120
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F handle() 3 92 19

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\Ui\ControlPanel\Component\Section\Command;
2
3
use Anomaly\Streams\Platform\Support\Authorizer;
4
use Anomaly\Streams\Platform\Ui\Breadcrumb\BreadcrumbCollection;
5
use Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section\Contract\SectionInterface;
6
use Anomaly\Streams\Platform\Ui\ControlPanel\ControlPanelBuilder;
7
use Illuminate\Http\Request;
8
9
/**
10
 * Class SetActiveSection
11
 *
12
 * @link   http://pyrocms.com/
13
 * @author PyroCMS, Inc. <[email protected]>
14
 * @author Ryan Thompson <[email protected]>
15
 */
16
class SetActiveSection
17
{
18
19
    /**
20
     * The control_panel builder.
21
     *
22
     * @var ControlPanelBuilder
23
     */
24
    protected $builder;
25
26
    /**
27
     * Create a new SetActiveSection instance.
28
     *
29
     * @param ControlPanelBuilder $builder
30
     */
31
    public function __construct(ControlPanelBuilder $builder)
32
    {
33
        $this->builder = $builder;
34
    }
35
36
    /**
37
     * Handle the command.
38
     *
39
     * @param Request              $request
40
     * @param Authorizer           $authorizer
41
     * @param BreadcrumbCollection $breadcrumbs
42
     */
43
    public function handle(Request $request, Authorizer $authorizer, BreadcrumbCollection $breadcrumbs)
44
    {
45
        $controlPanel = $this->builder->getControlPanel();
46
        $sections     = $controlPanel->getSections();
47
48
        /*
49
         * If we already have an active section
50
         * then we don't need to do this.
51
         */
52
        if ($active = $sections->active()) {
53
            return;
54
        }
55
56
        /* @var SectionInterface $section */
57
        foreach ($sections as $section) {
58
59
            if (($matcher = $section->getMatcher()) && str_is($matcher, $request->path())) {
60
                $active = $section;
61
            }
62
63
            /*
64
             * Get the HREF for both the active
65
             * and loop iteration section.
66
             */
67
            $href       = $section->getPermalink() ?: array_get($section->getAttributes(), 'href');
68
            $activeHref = '';
69
70
            if ($active && $active instanceof SectionInterface) {
71
                $activeHref = $active->getPermalink() ?: array_get($active->getAttributes(), 'href');
72
            }
73
74
            /*
75
             * If the request URL does not even
76
             * contain the HREF then skip it.
77
             */
78
            if (!str_contains($request->url(), $href)) {
79
                continue;
80
            }
81
82
            /*
83
             * Compare the length of the active HREF
84
             * and loop iteration HREF. The longer the
85
             * HREF the more detailed and exact it is and
86
             * the more likely it is the active HREF and
87
             * therefore the active section.
88
             */
89
            $hrefLength       = strlen($href);
90
            $activeHrefLength = strlen($activeHref);
91
92
            if ($hrefLength > $activeHrefLength) {
93
                $active = $section;
94
            }
95
        }
96
97
        /**
98
         * If we have an active section determined
99
         * then mark it as such.
100
         *
101
         * @var SectionInterface $active
102
         * @var SectionInterface $section
103
         */
104
        if ($active) {
105
            if ($active->getParent()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $active->getParent() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
106
                $active->setActive(true);
107
108
                $section = $sections->get($active->getParent(), $sections->first());
109
110
                $section->setHighlighted(true);
111
112
                $breadcrumbs->put($section->getBreadcrumb() ?: $section->getTitle(), $section->getHref());
113
            } else {
114
                $active->setActive(true)->setHighlighted(true);
115
            }
116
        } elseif ($active = $sections->first()) {
117
            $active->setActive(true)->setHighlighted(true);
118
        }
119
120
        // No active section!
121
        if (!$active) {
122
            return;
123
        }
124
125
        // Authorize the active section.
126
        if (!$authorizer->authorize($active->getPermission())) {
127
            abort(403);
128
        }
129
130
        // Add the bread crumb.
131 View Code Duplication
        if (($breadcrumb = $active->getBreadcrumb()) !== false) {
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...
132
            $breadcrumbs->put($breadcrumb ?: $active->getTitle(), $active->getHref());
133
        }
134
    }
135
}
136