Completed
Push — master ( e7ed50...ceaca0 )
by Ryan
02:06
created

PermissionFormBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 113
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
c 3
b 1
f 0
lcom 0
cbo 1
dl 113
loc 113
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onReady() 21 21 2
A onPost() 6 6 3
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\User\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 Anomaly\UsersModule\User\Contract\UserRepositoryInterface;
9
use Illuminate\Routing\Redirector;
10
11
/**
12
 * Class PermissionFormBuilder
13
 *
14
 * @link          http://anomaly.is/streams-platform
15
 * @author        AnomalyLabs, Inc. <[email protected]>
16
 * @author        Ryan Thompson <[email protected]>
17
 * @package       Anomaly\UsersModule\User\Permission
18
 */
19 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...
20
{
21
22
    /**
23
     * The addon to modify
24
     * permissions for.
25
     *
26
     * @var null|Addon
27
     */
28
    protected $addon = null;
29
30
    /**
31
     * No model needed.
32
     *
33
     * @var bool
34
     */
35
    protected $model = false;
36
37
    /**
38
     * Disable saving.
39
     *
40
     * @var bool
41
     */
42
    protected $save = false;
43
44
    /**
45
     * The form actions.
46
     *
47
     * @var array
48
     */
49
    protected $actions = [
50
        'save' => [
51
            'href' => 'admin/users/permissions/{request.route.parameters.id}'
52
        ]
53
    ];
54
55
    /**
56
     * The form options.
57
     *
58
     * @var array
59
     */
60
    protected $options = [
61
        'breadcrumb' => false,
62
        'permission' => 'anomaly.module.users::users.permissions'
63
    ];
64
65
    /**
66
     * Fired when builder is ready to build.
67
     *
68
     * @param UserRepositoryInterface $users
69
     * @param BreadcrumbCollection    $breadcrumbs
70
     * @param MessageBag              $messages
71
     * @param Redirector              $redirect
72
     * @return \Illuminate\Http\RedirectResponse
73
     */
74
    public function onReady(
75
        UserRepositoryInterface $users,
76
        RoleRepositoryInterface $roles,
77
        BreadcrumbCollection $breadcrumbs,
78
        MessageBag $messages,
79
        Redirector $redirect
80
    ) {
81
        $this->setEntry($user = $users->find($this->getEntry()));
82
83
        if ($user->hasRole($roles->findBySlug('admin'))) {
84
85
            $messages->warning('anomaly.module.users::warning.modify_admin_user');
86
87
            $this->setFormResponse($redirect->to('admin/users'));
88
89
            return;
90
        }
91
92
        $breadcrumbs->add($user->getDisplayName(), 'admin/users/edit/' . $user->getId());
93
        $breadcrumbs->add('anomaly.module.users::breadcrumb.permissions', 'admin/users/permissions/' . $user->getId());
94
    }
95
96
    /**
97
     * If nothing is posted then
98
     * the user gets no permissions.
99
     *
100
     * @param UserRepositoryInterface $users
101
     */
102
    public function onPost(UserRepositoryInterface $users)
103
    {
104
        if (!$this->hasPostData() && $entry = $this->getEntry()) {
105
            $users->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