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

ComponentController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 20.83 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 6
dl 20
loc 96
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getIndex() 0 4 1
A getEdit() 20 20 1
B postEdit() 0 45 5

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 Distilleries\Expendable\Http\Controllers\Backend;
2
3
use Distilleries\Expendable\Contracts\LayoutManagerContract;
4
use Distilleries\Expendable\Formatter\Message;
5
use Distilleries\Expendable\Http\Forms\Component\ComponentForm;
6
use Distilleries\Expendable\Http\Controllers\Backend\Base\BaseController;
7
use Distilleries\FormBuilder\Contracts\FormStateContract;
8
use Illuminate\Contracts\Console\Kernel;
9
use Illuminate\Http\Request;
10
use \FormBuilder;
11
12
class ComponentController extends BaseController implements FormStateContract {
13
14
    protected $artisan;
15
16
    public function __construct(Kernel $artisan, ComponentForm $form, LayoutManagerContract $layoutManager)
17
    {
18
        parent::__construct($layoutManager);
19
        $this->form    = $form;
0 ignored issues
show
Bug introduced by
The property form does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
        $this->artisan = $artisan;
21
    }
22
23
24
25
    // ------------------------------------------------------------------------------------------------
26
    // ------------------------------------------------------------------------------------------------
27
    // ------------------------------------------------------------------------------------------------
28
29
    public function getIndex()
30
    {
31
        return redirect()->to(action('\\' . get_class($this) . '@getEdit'));
32
    }
33
34
35
    // ------------------------------------------------------------------------------------------------
36
    // ------------------------------------------------------------------------------------------------
37
    // ------------------------------------------------------------------------------------------------
38
39 View Code Duplication
    public function getEdit($id = '')
40
    {
41
42
        $form = FormBuilder::create(get_class($this->form));
43
44
45
        $form_content = view('form-builder::form.components.formgenerator.full', [
46
            'form' => $form
47
        ]);
48
        $content      = view('expendable::admin.form.state.form', [
49
50
        ]);
51
52
        $this->layoutManager->add([
53
            'form'    => $form_content,
54
            'content' => $content,
55
        ]);
56
57
        return $this->layoutManager->render();
58
    }
59
60
    // ------------------------------------------------------------------------------------------------
61
62
    public function postEdit(Request $request)
63
    {
64
65
        $form = FormBuilder::create(get_class($this->form));
66
67
        if ($form->hasError())
68
        {
69
            return $form->validateAndRedirectBack();
70
        }
71
72
        $libelle            = $request->get('libelle');
73
        $libelle_form       = $libelle . 'Form';
74
        $libelle_datatable  = $libelle . 'Datatable';
75
        $libelle_controller = $libelle . 'Controller';
76
        $model              = $request->get('models');
77
        $states             = $request->get('state');
78
79
        foreach ($states as $state)
80
        {
81
            if (strpos($state, 'DatatableStateContract') !== false)
82
            {
83
                $this->artisan->call('datatable:make', [
84
                    '--fields' => $request->get('colon_datatable'),
85
                    'name'     => 'Http/Datatables/' . $libelle_datatable
86
                ]);
87
            } else if (strpos($state, 'FormStateContract') !== false)
88
            {
89
90
                $this->artisan->call('make:form', [
91
                    '--fields' => $request->get('fields_form'),
92
                    'name'     => 'Http/Forms/' . $libelle_form
93
                ]);
94
            }
95
        }
96
97
        $this->artisan->call('expendable:component.make', [
98
            '--states'    => join(',', $states),
99
            '--model'     => $model,
100
            '--datatable' => $libelle_datatable,
101
            '--form'      => $libelle_form,
102
            'name'        => 'Http/Controllers/Admin/' . $libelle_controller
103
        ]);
104
105
        return redirect()->back()->with(Message::MESSAGE, [trans('expendable::success.generated')]);
106
    }
107
}