Completed
Branch master (a6481d)
by Fèvre
02:09
created

AccountController::update()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 1
1
<?php
2
namespace Xetaravel\Http\Controllers;
3
4
use Illuminate\Http\Request;
5
use Illuminate\Http\RedirectResponse;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\View\View;
8
use Xetaravel\Models\Repositories\AccountRepository;
9
use Xetaravel\Models\User;
10
use Xetaravel\Models\Validators\AccountValidator;
11
12
class AccountController extends Controller
13
{
14
    /**
15
     * Constructor
16
     */
17
    public function __construct()
18
    {
19
        parent::__construct();
20
21
        $this->breadcrumbs->addCrumb('Account', route('users_account_index'));
0 ignored issues
show
Bug introduced by
The property breadcrumbs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    }
23
24
    /**
25
     * Show the account update form.
26
     *
27
     * @return \Illuminate\View\View
28
     */
29
    public function index(): View
30
    {
31
        $user = User::find(Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
32
33
        $this->breadcrumbs->setCssClasses('breadcrumb');
34
35
        return view('account.index', ['user' => $user, 'breadcrumbs' => $this->breadcrumbs]);
36
    }
37
38
    /**
39
     * Handle a account update request for the application.
40
     *
41
     * @param \Illuminate\Http\Request $request
42
     *
43
     * @return \Illuminate\Http\RedirectResponse
44
     */
45
    public function update(Request $request): RedirectResponse
46
    {
47
        AccountValidator::update($request->all())->validate();
48
49
        if (AccountRepository::update($request->all())) {
50
            $user = User::find(Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
51
52
            // Handle the avatar upload.
53
            if (!is_null($request->file('avatar'))) {
54
                $user->clearMediaCollection('avatar');
55
                $user->addMedia($request->file('avatar'))
56
                    ->preservingOriginal()
57
                    ->setName(substr(md5($user->username), 0, 10))
58
                    ->setFileName(substr(md5($user->username), 0, 10) . '.' . $request->file('avatar')->extension())
59
                    ->toMediaCollection('avatar');
60
            }
61
62
            return redirect()
63
                ->route('users_account_index')
64
                ->with('success', 'Your account has been updated successfully !');
65
        } else {
66
            return redirect()
67
                ->route('users_account_index')
68
                ->with('danger', 'An error occurred while saving your informations !');
69
        }
70
    }
71
}
72