FieldsController::destroy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\Http\Controllers\Core;
4
5
use Illuminate\Http\Request;
6
use Hechoenlaravel\JarvisFoundation\Traits\EntityManager;
7
use Hechoenlaravel\JarvisFoundation\FieldGenerator\FieldModel;
8
use Hechoenlaravel\JarvisFoundation\Http\Controllers\Controller;
9
use Hechoenlaravel\JarvisFoundation\Http\Requests\EditFieldRequest;
10
use Hechoenlaravel\JarvisFoundation\Http\Requests\CreateFieldRequest;
11
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
12
use Hechoenlaravel\JarvisFoundation\FieldGenerator\Transformers\FieldTransformer;
13
14
/**
15
 * Class FieldsController
16
 * @package Hechoenlaravel\JarvisFoundation\Http\Controllers\Core
17
 */
18
class FieldsController extends Controller
19
{
20
    use EntityManager;
21
22
23
    /**
24
     * @var FieldModel
25
     */
26
    protected $model;
27
28
    /**
29
     * @var
30
     */
31
    protected $fieldType;
32
33
    /**
34
     * FieldsController constructor.
35
     * @param FieldModel $model
36
     */
37
    public function __construct(FieldModel $model)
38
    {
39
        $this->model = $model;
40
    }
41
42
    /**
43
     * @param $id
44
     * @return \Illuminate\Http\JsonResponse
45
     */
46
    public function index($id)
47
    {
48
        $model = $this->model->byEntity($id)->orderBy('order', 'ASC');
49
        return response()->json(fractal()->collection($model->get(), new FieldTransformer())->toArray());
0 ignored issues
show
Bug introduced by
It seems like fractal()->collection($m...ansformer())->toArray() targeting Spatie\Fractalistic\Fractal::toArray() can also be of type null; however, Illuminate\Contracts\Rou...ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
    }
51
52
    /**
53
     * @param CreateFieldRequest $request
54
     * @param $id
55
     * @return \Illuminate\Http\JsonResponse
56
     */
57 View Code Duplication
    public function store(CreateFieldRequest $request, $id)
0 ignored issues
show
Duplication introduced by
This method 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...
58
    {
59
        $data = $request->all();
60
        $data['entity_id'] = $id;
61
        $field = $this->generateField($data);
62
        $response = fractal()->item($field, new FieldTransformer())
63
            ->addMeta(['return_url' => $data['returnUrl']])
64
            ->toArray();
65
        return response()->json($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by fractal()->item($field, ...eturnUrl']))->toArray() on line 62 can also be of type null; however, Illuminate\Contracts\Rou...ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
66
    }
67
68
    /**
69
     * @param $id
70
     * @return \Illuminate\Http\JsonResponse
71
     */
72
    public function show($id)
73
    {
74
        $response = fractal()->item($this->model->findOrFail($id), new FieldTransformer())->toArray();
75
        return response()->json($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by fractal()->item($this->m...ansformer())->toArray() on line 74 can also be of type null; however, Illuminate\Contracts\Rou...ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76
    }
77
78
79
    /**
80
     * @param EditFieldRequest $request
81
     * @param $id
82
     * @param $fields
83
     * @return \Illuminate\Http\JsonResponse
84
     */
85 View Code Duplication
    public function update(EditFieldRequest $request, $id, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method 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...
86
    {
87
        $data = $request->all();
88
        $data['id'] = $fields;
89
        $field = $this->editField($data);
90
        $response = fractal()->item($field, new FieldTransformer())
91
            ->addMeta(['return_url' => $data['returnUrl']])
92
            ->toArray();
93
        return response()->json($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by fractal()->item($field, ...eturnUrl']))->toArray() on line 90 can also be of type null; however, Illuminate\Contracts\Rou...ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
94
    }
95
96
97
    /**
98
     * @param $id
99
     * @param $fields
100
     * @return \Illuminate\Http\JsonResponse
101
     */
102
    public function destroy($id, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        $this->deleteField($fields);
105
        return response()->json(null, 204);
106
    }
107
108
109
    /**
110
     * @param $type
111
     * @return \Illuminate\Http\JsonResponse
112
     */
113
    public function fieldTypeForm($type)
114
    {
115
        $this->setFieldType($type);
116
        return response()->json(['form' => $this->fieldType->getOptionsForm()]);
117
    }
118
119
120
    /**
121
     * @param $type
122
     */
123
    private function setFieldType($type)
124
    {
125
        $fieldTypes = app('field.types');
126
        if (!isset($fieldTypes->types[$type])) {
127
            throw new NotAcceptableHttpException('El field type ' . $type . ' no esta registrado');
128
        }
129
        $this->fieldType = app($fieldTypes->types[$type]);
130
    }
131
132
133
    /**
134
     * @param Request $request
135
     * @param $id
136
     * @return \Illuminate\Http\JsonResponse
137
     */
138
    public function reOrderFieldId(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        $this->reOrderField($request->get('items'));
141
        return response()->json(['ok' => true]);
142
    }
143
}
144