Passed
Push — master ( b24b06...03dd5d )
by F
03:26
created

LanguageController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PWWEB\Localisation\Controllers;
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\Middleware\Locale;
9
use PWWEB\Localisation\Repositories\LanguageRepository;
10
use PWWEB\Localisation\Requests\CreateLanguageRequest;
11
use PWWEB\Localisation\Requests\UpdateLanguageRequest;
12
13
/**
14
 * PWWEB\Localisation\Controllers\LanguageController LanguageController.
15
 *
16
 * The CRUD controller for Language
17
 * Class LanguageController
18
 *
19
 * @package   pwweb/localisation
20
 * @author    Frank Pillukeit <[email protected]>
21
 * @author    Richard Browne <[email protected]
22
 * @copyright 2020 pw-websolutions.com
23
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
24
 */
25
class LanguageController extends Controller
26
{
27
    /**
28
     * Repository of Languages to be used throughout the controller.
29
     *
30
     * @var \PWWEB\Localisation\Repositories\LanguageRepository
31
     */
32
    private $languageRepository;
33
34
    /**
35
     * Constructor for the Language controller.
36
     *
37
     * @param \PWWEB\Localisation\Repositories\LanguageRepository $languageRepo Repository of Languages
38
     */
39
    public function __construct(LanguageRepository $languageRepo)
40
    {
41
        $this->languageRepository = $languageRepo;
42
    }
43
44
    /**
45
     * Display a listing of the Language.
46
     *
47
     * @param Request $request Request containing the information for filtering.
48
     *
49
     * @return \Illuminate\View\View
50
     */
51
    public function index(Request $request)
52
    {
53
        $languages = $this->languageRepository->all();
54
55
        return view('localisation::languages.index')
56
            ->with('languages', $languages);
57
    }
58
59
    /**
60
     * Show the form for creating a new Language.
61
     *
62
     * @return \Illuminate\View\View
63
     */
64
    public function create()
65
    {
66
        return view('localisation::languages.create');
67
    }
68
69
    /**
70
     * Store a newly created Language in storage.
71
     *
72
     * @param CreateLanguageRequest $request Request containing the information to be stored.
73
     *
74
     * @return \Illuminate\Http\RedirectResponse
75
     */
76
    public function store(CreateLanguageRequest $request)
77
    {
78
        $input = $request->all();
79
80
        $language = $this->languageRepository->create($input);
0 ignored issues
show
Unused Code introduced by
The assignment to $language is dead and can be removed.
Loading history...
81
82
        Flash::success('Language saved successfully.');
83
84
        return redirect(route('localisation.languages.index'));
85
    }
86
87
    /**
88
     * Display the specified Language.
89
     *
90
     * @param int $id ID of the Language to be displayed. Used for retrieving currently held data.
91
     *
92
     * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
93
     */
94
    public function show($id)
95
    {
96
        $language = $this->languageRepository->find($id);
97
98
        if (true === empty($language)) {
99
            Flash::error('Language not found');
100
101
            return redirect(route('localisation.languages.index'));
102
        }
103
104
        return view('localisation::languages.show')
105
            ->with('language', $language);
106
    }
107
108
    /**
109
     * Show the form for editing the specified Language.
110
     *
111
     * @param int $id ID of the Language to be edited. Used for retrieving currently held data.
112
     *
113
     * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
114
     */
115
    public function edit($id)
116
    {
117
        $language = $this->languageRepository->find($id);
118
119
        if (true === empty($language)) {
120
            Flash::error('Language not found');
121
122
            return redirect(route('localisation.languages.index'));
123
        }
124
125
        return view('localisation::languages.edit')->with('language', $language);
126
    }
127
128
    /**
129
     * Update the specified Language in storage.
130
     *
131
     * @param int                   $id      ID of the Language to be updated.
132
     * @param UpdateLanguageRequest $request Request containing the information to be updated.
133
     *
134
     * @return \Illuminate\Http\RedirectResponse
135
     */
136
    public function update($id, UpdateLanguageRequest $request)
137
    {
138
        $language = $this->languageRepository->find($id);
139
140
        if (true === empty($language)) {
141
            Flash::error('Language not found');
142
143
            return redirect(route('localisation.languages.index'));
144
        }
145
146
        $language = $this->languageRepository->update($request->all(), $id);
0 ignored issues
show
Unused Code introduced by
The assignment to $language is dead and can be removed.
Loading history...
147
148
        Flash::success('Language updated successfully.');
149
150
        return redirect(route('localisation.languages.index'));
151
    }
152
153
    /**
154
     * Remove the specified Language from storage.
155
     *
156
     * @param int $id ID of the Language to be destroyed.
157
     *
158
     * @throws \Exception
159
     *
160
     * @return \Illuminate\Http\RedirectResponse
161
     */
162
    public function destroy($id)
163
    {
164
        $language = $this->languageRepository->find($id);
165
166
        if (true === empty($language)) {
167
            Flash::error('Language not found');
168
169
            return redirect(route('localisation.languages.index'));
170
        }
171
172
        $this->languageRepository->delete($id);
173
174
        Flash::success('Language deleted successfully.');
175
176
        return redirect(route('localisation.languages.index'));
177
    }
178
179
    /**
180
     * Switch the locale.
181
     *
182
     * @param string $locale Locale to change to.
183
     *
184
     * @return \Illuminate\Http\RedirectResponse
185
     */
186
    public function changeLocale($locale)
187
    {
188
        $locales = (array) $this->languageRepository->getAllActive();
189
190
        // If a locale does not match any of the ones allowed, go back without doing anything.
191
        if (false === in_array($locale, $locales)) {
192
            return redirect()->back();
193
        }
194
195
        // Set the right sessions.
196
        session([Locale::SESSION_KEY => $locale]);
197
        app()->setLocale($locale);
0 ignored issues
show
introduced by
The method setLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

197
        app()->/** @scrutinizer ignore-call */ setLocale($locale);
Loading history...
198
        // \LangCountry::setAllSessions($lang_country);
199
200
        // If a user is logged in and it has a lang_country property, set the new lang_country.
201
        /*
202
         * Todo Set language to user options
203
         * if (Auth::user() && array_key_exists('lang_country', Auth::user()->getAttributes())) {
204
         *
205
            try {
206
                \Auth::user()->lang_country = $lang_country;
207
                \Auth::user()->save();
208
            } catch (\Exception $e) {
209
                \Log::error(get_class($this).' at '.__LINE__.': '.$e->getMessage());
210
            }
211
        }*/
212
213
        return redirect()->back();
214
    }
215
}
216