PermissionFormBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 115
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 115
loc 115
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onPost() 6 6 3
A onReady() 23 23 2
A getAddon() 4 4 1
A setAddon() 6 6 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\UsersModule\Role\Permission;
2
3
use Anomaly\Streams\Platform\Addon\Addon;
4
use Anomaly\Streams\Platform\Message\MessageBag;
5
use Anomaly\Streams\Platform\Ui\Breadcrumb\BreadcrumbCollection;
6
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
7
use Anomaly\UsersModule\Role\Contract\RoleRepositoryInterface;
8
use Illuminate\Routing\Redirector;
9
10
/**
11
 * Class PermissionFormBuilder
12
 *
13
 * @link          http://pyrocms.com/
14
 * @author        PyroCMS, Inc. <[email protected]>
15
 * @author        Ryan Thompson <[email protected]>
16
 */
17 View Code Duplication
class PermissionFormBuilder extends FormBuilder
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
20
    /**
21
     * The addon to modify
22
     * permissions for.
23
     *
24
     * @var null|Addon
25
     */
26
    protected $addon = null;
27
28
    /**
29
     * No model needed.
30
     *
31
     * @var bool
32
     */
33
    protected $model = false;
34
35
    /**
36
     * Disable saving.
37
     *
38
     * @var bool
39
     */
40
    protected $save = false;
41
42
    /**
43
     * The form actions.
44
     *
45
     * @var array
46
     */
47
    protected $actions = [
48
        'save' => [
49
            'href' => 'admin/users/permissions/{request.route.parameters.id}',
50
        ],
51
    ];
52
53
    /**
54
     * The form options.
55
     *
56
     * @var array
57
     */
58
    protected $options = [
59
        'breadcrumb' => false,
60
        'permission' => 'anomaly.module.users::users.permissions',
61
    ];
62
63
    /**
64
     * Fired when builder is ready to build.
65
     *
66
     * @param  RoleRepositoryInterface           $roles
67
     * @param  BreadcrumbCollection              $breadcrumbs
68
     * @param  MessageBag                        $messages
69
     * @param  Redirector                        $redirect
70
     * @return \Illuminate\Http\RedirectResponse
71
     */
72
    public function onReady(
73
        RoleRepositoryInterface $roles,
74
        BreadcrumbCollection $breadcrumbs,
75
        MessageBag $messages,
76
        Redirector $redirect
77
    ) {
78
        $this->setEntry($role = $roles->find($this->getEntry()));
79
80
        if ($role->getSlug() === 'admin') {
81
82
            $messages->warning('anomaly.module.users::warning.modify_admin_role');
83
84
            $this->setFormResponse($redirect->to('admin/users/roles'));
85
86
            return;
87
        }
88
89
        $breadcrumbs->add($role->getName(), 'admin/users/roles/edit/' . $role->getId());
90
        $breadcrumbs->add(
91
            'anomaly.module.users::breadcrumb.permissions',
92
            'admin/users/roles/permissions/' . $role->getId()
93
        );
94
    }
95
96
    /**
97
     * If nothing is posted then
98
     * the role gets no permissions.
99
     *
100
     * @param RoleRepositoryInterface $roles
101
     */
102
    public function onPost(RoleRepositoryInterface $roles)
103
    {
104
        if (!$this->hasPostData() && $entry = $this->getEntry()) {
105
            $roles->save($entry->setAttribute('permissions', []));
106
        }
107
    }
108
109
    /**
110
     * Get the addon.
111
     *
112
     * @return null|Addon
113
     */
114
    public function getAddon()
115
    {
116
        return $this->addon;
117
    }
118
119
    /**
120
     * Set the addon.
121
     *
122
     * @param  Addon $addon
123
     * @return $this
124
     */
125
    public function setAddon(Addon $addon)
126
    {
127
        $this->addon = $addon;
128
129
        return $this;
130
    }
131
}
132