RateController::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 15
rs 10
1
<?php
2
3
namespace PWWEB\Localisation\Controllers\Tax;
4
5
use App\Http\Controllers\Controller;
6
use Flash;
0 ignored issues
show
Bug introduced by
The type Flash was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Http\Request;
8
use PWWEB\Localisation\Enums\Tax\Type;
9
use PWWEB\Localisation\Repositories\Tax\RateRepository;
10
use PWWEB\Localisation\Requests\Tax\CreateRateRequest;
11
use PWWEB\Localisation\Requests\Tax\UpdateRateRequest;
12
13
/**
14
 * PWWEB\Localisation\Controllers\Tax\RateController RateController.
15
 *
16
 * The CRUD controller for Rate
17
 * Class RateController
18
 *
19
 * @author    Frank Pillukeit <[email protected]>
20
 * @author    Richard Browne <[email protected]
21
 * @copyright 2020 pw-websolutions.com
22
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
23
 */
24
class RateController extends Controller
25
{
26
    /**
27
     * Repository of rates to be used throughout the controller.
28
     *
29
     * @var \PWWEB\Localisation\Repositories\Tax\RateRepository
30
     */
31
    private $rateRepository;
32
33
    /**
34
     * Constructor for the Rate controller.
35
     *
36
     * @param RateRepository $rateRepo Repository of Rates.
37
     */
38
    public function __construct(RateRepository $rateRepo)
39
    {
40
        $this->rateRepository = $rateRepo;
41
    }
42
43
    /**
44
     * Display a listing of the Rate.
45
     *
46
     * @param Request $request Index request
47
     *
48
     * @return \Illuminate\View\View
49
     */
50
    public function index(Request $request)
51
    {
52
        $rates = $this->rateRepository->paginate(15);
53
54
        return view('localisation::tax.rates.index')
55
            ->with('rates', $rates);
56
    }
57
58
    /**
59
     * Show the form for creating a new Rate.
60
     *
61
     * @return \Illuminate\View\View
62
     */
63
    public function create()
64
    {
65
        $types = Type::getAll();
66
67
        return view('localisation::tax.rates.create', compact('types'));
68
    }
69
70
    /**
71
     * Store a newly created Rate in storage.
72
     *
73
     * @param CreateRateRequest $request Create Request
74
     *
75
     * @return \Illuminate\View\View
76
     */
77
    public function store(CreateRateRequest $request)
78
    {
79
        $input = $request->all();
80
81
        $rate = $this->rateRepository->create($input);
0 ignored issues
show
Unused Code introduced by
The assignment to $rate is dead and can be removed.
Loading history...
82
83
        Flash::success(__('pwweb::localisation.tax.rates.saved'));
84
85
        return redirect(route('localisation.tax.rates.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('l...tion.tax.rates.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
86
    }
87
88
    /**
89
     * Display the specified Rate.
90
     *
91
     * @param int $id ID to show
92
     *
93
     * @return \Illuminate\View\View
94
     */
95
    public function show($id)
96
    {
97
        $rate = $this->rateRepository->find($id);
98
        $rate->type = Type::make($rate->type);
0 ignored issues
show
Bug introduced by
It seems like $rate->type can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $value of Spatie\Enum\Enum::make() does only seem to accept integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        $rate->type = Type::make(/** @scrutinizer ignore-type */ $rate->type);
Loading history...
99
100
        if (true === empty($rate)) {
101
            Flash::error(__('pwweb::localisation.tax.rates.not_found'));
102
103
            return redirect(route('localisation.tax.rates.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('l...tion.tax.rates.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
104
        }
105
106
        return view('localisation::tax.rates.show')->with('rate', $rate);
107
    }
108
109
    /**
110
     * Show the form for editing the specified Rate.
111
     *
112
     * @param int $id ID to edit
113
     *
114
     * @return \Illuminate\View\View
115
     */
116
    public function edit($id)
117
    {
118
        $rate = $this->rateRepository->find($id);
119
        $types = Type::getAll();
120
121
        if (true === empty($rate)) {
122
            Flash::error(__('pwweb::localisation.tax.rates.not_found'));
123
124
            return redirect(route('localisation.tax.rates.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('l...tion.tax.rates.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
125
        }
126
127
        return view('localisation::tax.rates.edit', compact('rate', 'types'));
128
    }
129
130
    /**
131
     * Update the specified Rate in storage.
132
     *
133
     * @param int               $id      ID to update
134
     * @param UpdateRateRequest $request Edit Request
135
     *
136
     * @return \Illuminate\View\View
137
     */
138
    public function update($id, UpdateRateRequest $request)
139
    {
140
        $rate = $this->rateRepository->find($id);
141
142
        if (true === empty($rate)) {
143
            Flash::error(__('pwweb::localisation.tax.rates.not_found'));
144
145
            return redirect(route('localisation.tax.rates.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('l...tion.tax.rates.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
146
        }
147
148
        $rate = $this->rateRepository->update($request->all(), $id);
0 ignored issues
show
Unused Code introduced by
The assignment to $rate is dead and can be removed.
Loading history...
149
150
        Flash::success(__('pwweb::localisation.tax.rates.updated'));
151
152
        return redirect(route('localisation.tax.rates.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('l...tion.tax.rates.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
153
    }
154
155
    /**
156
     * Remove the specified Rate from storage.
157
     *
158
     * @param int $id ID to destroy
159
     *
160
     * @throws \Exception
161
     *
162
     * @return \Illuminate\View\View
163
     */
164
    public function destroy($id)
165
    {
166
        $rate = $this->rateRepository->find($id);
167
168
        if (true === empty($rate)) {
169
            Flash::error(__('pwweb::localisation.tax.rates.not_found'));
170
171
            return redirect(route('localisation.tax.rates.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('l...tion.tax.rates.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
172
        }
173
174
        $this->rateRepository->delete($id);
175
176
        Flash::success(__('pwweb::localisation.tax.rates.deleted'));
177
178
        return redirect(route('localisation.tax.rates.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('l...tion.tax.rates.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
179
    }
180
}
181