Code Duplication    Length = 113-115 lines in 2 locations

src/Role/Permission/PermissionFormBuilder.php 1 location

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

src/User/Permission/PermissionFormBuilder.php 1 location

@@ 19-131 (lines=113) @@
16
 * @author        Ryan Thompson <[email protected]>
17
 * @package       Anomaly\UsersModule\User\Permission
18
 */
19
class PermissionFormBuilder extends FormBuilder
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