Completed
Push — master ( e5ba92...7a1e6c )
by Arjay
13:54
created

ProfileController::removeAvatar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use Yajra\CMS\Http\Requests\ProfileFormRequest;
6
use App\Http\Requests;
7
8
class ProfileController extends Controller
9
{
10
    /**
11
     * Show the form for editing the specified resource.
12
     *
13
     * @return \Illuminate\Http\Response
14
     */
15
    public function edit()
16
    {
17
        return view('administrator.profile.edit');
18
    }
19
20
    /**
21
     * Update the specified resource in storage.
22
     *
23
     * @param  \Yajra\CMS\Http\Requests\ProfileFormRequest $request
24
     * @return \Illuminate\Http\Response
25
     * @throws \Laracasts\Presenter\Exceptions\PresenterException
26
     */
27
    public function update(ProfileFormRequest $request)
28
    {
29
        $profile = $request->user();
30
        $profile->fill($request->only(['first_name', 'last_name', 'email']));
31
32
        if ($request->get('password')) {
33
            $profile->password = bcrypt($request->get('password'));
34
        }
35
36
        if ((null !== $request->file('avatar')) && $request->file('avatar')->isValid()) {
37
            $filename = $this->getFilename($request);
38
            $request->file('avatar')->move('img/avatar', $filename);
39
            $profile->avatar = url('img/avatar/' . $filename);
40
        }
41
42
        $profile->save();
43
44
        flash()->success(trans('cms::profile.alert.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::profile.alert.success') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, 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...
45
46
        return redirect()->route('administrator.profile.edit');
47
    }
48
49
    /**
50
     * @param \Yajra\CMS\Http\Requests\ProfileFormRequest $request
51
     * @return string
52
     * @throws \Laracasts\Presenter\Exceptions\PresenterException
53
     */
54
    protected function getFilename(ProfileFormRequest $request)
55
    {
56
        $fileName = $request->user()->id . '.' . $request->file('avatar')->getClientOriginalExtension();
57
58
        return $fileName;
59
    }
60
61
    /**
62
     * Remove current user's avatar.
63
     *
64
     * @return \Illuminate\Http\RedirectResponse
65
     */
66
    public function removeAvatar()
67
    {
68
        $profile = auth()->user();
0 ignored issues
show
Bug introduced by
The method user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        $profile->avatar = '';
70
        $profile->save();
71
72
        return redirect()->route('administrator.profile.edit');
73
    }
74
}
75