PermissionFormBuilder::onReady()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 21
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 21
loc 21
c 1
b 0
f 0
rs 9.3142
cc 2
eloc 13
nc 2
nop 5
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://pyrocms.com/
15
 * @author        PyroCMS, Inc. <[email protected]>
16
 * @author        Ryan Thompson <[email protected]>
17
 */
18 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...
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  UserRepositoryInterface           $users
68
     * @param  BreadcrumbCollection              $breadcrumbs
69
     * @param  MessageBag                        $messages
70
     * @param  Redirector                        $redirect
71
     * @return \Illuminate\Http\RedirectResponse
72
     */
73
    public function onReady(
74
        UserRepositoryInterface $users,
75
        RoleRepositoryInterface $roles,
76
        BreadcrumbCollection $breadcrumbs,
77
        MessageBag $messages,
78
        Redirector $redirect
79
    ) {
80
        $this->setEntry($user = $users->find($this->getEntry()));
81
82
        if ($user->hasRole($roles->findBySlug('admin'))) {
83
84
            $messages->warning('anomaly.module.users::warning.modify_admin_user');
85
86
            $this->setFormResponse($redirect->to('admin/users'));
87
88
            return;
89
        }
90
91
        $breadcrumbs->add($user->getDisplayName(), 'admin/users/edit/' . $user->getId());
92
        $breadcrumbs->add('anomaly.module.users::breadcrumb.permissions', 'admin/users/permissions/' . $user->getId());
93
    }
94
95
    /**
96
     * If nothing is posted then
97
     * the user gets no permissions.
98
     *
99
     * @param UserRepositoryInterface $users
100
     */
101
    public function onPost(UserRepositoryInterface $users)
102
    {
103
        if (!$this->hasPostData() && $entry = $this->getEntry()) {
104
            $users->save($entry->setAttribute('permissions', []));
105
        }
106
    }
107
108
    /**
109
     * Get the addon.
110
     *
111
     * @return null|Addon
112
     */
113
    public function getAddon()
114
    {
115
        return $this->addon;
116
    }
117
118
    /**
119
     * Set the addon.
120
     *
121
     * @param  Addon $addon
122
     * @return $this
123
     */
124
    public function setAddon(Addon $addon)
125
    {
126
        $this->addon = $addon;
127
128
        return $this;
129
    }
130
}
131