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

PermissionFormBuilder::onPost()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 6
rs 9.4285
cc 3
eloc 3
nc 2
nop 1
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://anomaly.is/streams-platform
14
 * @author        AnomalyLabs, Inc. <[email protected]>
15
 * @author        Ryan Thompson <[email protected]>
16
 * @package       Anomaly\UsersModule\Role\Permission
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 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