Completed
Push — master ( 188ffb...31684b )
by Maxime
02:48
created

PermissionController::postEdit()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.5806
cc 4
eloc 14
nc 4
nop 1
1
<?php namespace Distilleries\Expendable\Http\Controllers\Backend;
2
3
use Distilleries\Expendable\Contracts\LayoutManagerContract;
4
use Distilleries\Expendable\Http\Forms\Permission\PermissionForm;
5
use Distilleries\Expendable\Http\Controllers\Backend\Base\ModelBaseController;
6
use Distilleries\Expendable\States\FormStateTrait;
7
use Illuminate\Http\Request;
8
use Distilleries\Expendable\Models\Permission;
9
use Distilleries\FormBuilder\Contracts\FormStateContract;
10
use \FormBuilder;
11
12
class PermissionController extends ModelBaseController  implements FormStateContract {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before implements keyword; 2 found
Loading history...
13
14
    use FormStateTrait;
15
16
    public function __construct(PermissionForm $form, Permission $model, LayoutManagerContract $layoutManager)
17
    {
18
        parent::__construct($model, $layoutManager);
19
        $this->form = $form;
20
    }
21
22
23
    // ------------------------------------------------------------------------------------------------
24
    // ------------------------------------------------------------------------------------------------
25
    // ------------------------------------------------------------------------------------------------
26
27
    public function getIndex()
28
    {
29
        return redirect()->to(action('\\' . get_class($this) . '@getEdit'));
30
    }
31
32
    // ------------------------------------------------------------------------------------------------
33
34
    public function postEdit(Request $request)
35
    {
36
37
        $form = FormBuilder::create(get_class($this->form), [
38
            'model' => $this->model
39
        ]);
40
41
        if ($form->hasError())
42
        {
43
            return $form->validateAndRedirectBack();
44
        }
45
46
        $permissions = $request->get('permission');
47
        $this->model->truncate();
0 ignored issues
show
Documentation Bug introduced by
The method truncate does not exist on object<Distilleries\Expendable\Models\BaseModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
48
49
50
        foreach ($permissions as $role_id=>$permission) {
51
52
            foreach ($permission as $service_id) {
53
                $permModel = new $this->model;
54
                $permModel->role_id = $role_id;
55
                $permModel->service_id = $service_id;
56
                $permModel->save();
57
            }
58
        }
59
60
61
        return redirect()->to(action('\\' . get_class($this) . '@getIndex'));
62
63
    }
64
}