ProvidersController::edit()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
crap 12
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\ProviderCreateRequest;
6
use App\Http\Requests\ProviderUpdateRequest;
7
use App\Repositories\ProviderRepository;
8
use App\Validators\ProviderValidator;
9
use Illuminate\Http\Request;
10
11
class ProvidersController extends Controller
12
{
13
    /**
14
     * @var ProviderRepository
15
     */
16
    protected $repository;
17
18
    /**
19
     * @var ProviderValidator
20
     */
21
    protected $validator;
22
23
    public function __construct(ProviderRepository $repository, ProviderValidator $validator)
24
    {
25
        $this->repository = $repository;
26
        $this->validator = $validator;
27
    }
28
29
    /**
30
     * Display a listing of the resource.
31
     *
32
     * @return \Illuminate\Http\Response
33
     */
34
    public function index()
35
    {
36
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
37
        $providers = $this->repository->all();
38
39
        if (request()->wantsJson()) {
40
            return response()->json([
41
                'data' => $providers,
42
            ]);
43
        }
44
45
        $providers = Country::paginate(5);
46
47
        return view('manteniments/providers/index', ['providers' => $providers]);
48
49
        //return view('providers.index', compact('providers'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
    }
51
52
    /**
53
     * Store a newly created resource in storage.
54
     *
55
     * @param ProviderCreateRequest $request
56
     *
57
     * @return \Illuminate\Http\Response
58
     */
59
    public function store(ProviderCreateRequest $request)
60
    {
61
        return view('manteniments/providers/create');
62
    }
63
64
    /**
65
     * Display the specified resource.
66
     *
67
     * @param int $id
68
     *
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function show($id)
72
    {
73
        $this->validateInput($request);
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
74
        Country::create([
75
        'name'          => $request['name'],
76
        'shortName'     => $request['shortName'],
77
        'description'   => $request['description'],
78
        'date_entrance' => $request['date_entrance'],
79
        'last_update'   => $request['last_update'],
80
          ]);
81
82
        return redirect()->intended('manteniments/providers');
83
    }
84
85
    /**
86
     * Show the form for editing the specified resource.
87
     *
88
     * @param int $id
89
     *
90
     * @return \Illuminate\Http\Response
91
     */
92
    public function edit($id)
93
    {
94
        $providers = Providers::find($id);
95
      // Redirect to country list if updating country wasn't existed
96
      if ($providers == null || count($providers) == 0) {
97
          return redirect()->intended('/manteniments/providers');
98
      }
99
100
        return view('manteniments/providers/edit', ['providers' => $providers]);
101
    }
102
103
    /**
104
     * Update the specified resource in storage.
105
     *
106
     * @param ProviderUpdateRequest $request
107
     * @param string                $id
108
     *
109
     * @return Response
110
     */
111
    public function update(ProviderUpdateRequest $request, $id)
112
    {
113
        $providers = Providers::findOrFail($id);
0 ignored issues
show
Unused Code introduced by
$providers is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
        $input = [
115
        'name'          => $request['name'],
116
        'shortName'     => $request['shortName'],
117
        'description'   => $request['description'],
118
        'date_entrance' => $request['date_entrance'],
119
        'last_update'   => $request['last_update'],
120
      ];
121
        $this->validate($request, [
122
      'name' => 'required|max:60',
123
      ]);
124
        Providers::where('id', $id)
125
          ->update($input);
126
127
        return redirect()->intended('manteniments/providers');
128
    }
129
130
    /**
131
     * Remove the specified resource from storage.
132
     *
133
     * @param int $id
134
     *
135
     * @return \Illuminate\Http\Response
136
     */
137
    public function destroy($id)
138
    {
139
        Providers::where('id', $id)->delete();
140
141
        return redirect()->intended('manteniments/providers');
142
    }
143
144
    public function search(Request $request)
145
    {
146
        $constraints = [
147
            'name'      => $request['name'],
148
            'shortName' => $request['shortName'],
149
            ];
150
        $providers = $this->doSearchingQuery($constraints);
151
152
        return view('manteniments/providers/index', ['providers' => $providers, 'searchingVals' => $constraints]);
153
    }
154
155
    private function doSearchingQuery($constraints)
156
    {
157
        $query = providers::query();
158
        $fields = array_keys($constraints);
159
        $index = 0;
160
        foreach ($constraints as $constraint) {
161
            if ($constraint != null) {
162
                $query = $query->where($fields[$index], 'like', '%'.$constraint.'%');
163
            }
164
            $index++;
165
        }
166
167
        return $query->paginate(5);
168
    }
169
170
    private function validateInput($request)
171
    {
172
        $this->validate($request, [
173
        'name'      => 'required|max:60|unique:providers',
174
        'shortName' => 'required|max:6|unique:providers',
175
    ]);
176
    }
177
}
178