MembersController::getEventDescriptionFor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Back;
4
5
use App\Services\Auth\Front\User;
6
use App\Services\Auth\Front\Enums\UserRole;
7
use App\Http\Requests\Back\FrontUserRequest;
8
use App\Services\Auth\Front\Enums\UserStatus;
9
use App\Services\Auth\Front\Events\UserCreatedThroughBack;
10
11
class MembersController
12
{
13
    public function index()
14
    {
15
        $users = User::all();
16
17
        return view('back.members.index')->with(compact('users'));
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
18
    }
19
20
    public function create()
21
    {
22
        return view('back.members.create', ['user' => new User()]);
23
    }
24
25
    public function store(FrontUserRequest $request)
26
    {
27
        $user = new User();
28
29
        $user->email = $request->get('email');
30
        $user->first_name = $request->get('first_name');
31
        $user->last_name = $request->get('last_name');
32
        $user->locale = $request->get('locale', 'nl');
33
34
        $user->role = UserRole::MEMBER;
35
        $user->status = UserStatus::ACTIVE;
36
37
        $user->save();
38
39
        $eventDescription = $this->getEventDescriptionFor('created', $user);
40
        activity($eventDescription);
41
        flash()->success(strip_tags($eventDescription).'. '.fragment('back.members.passwordMailSent'));
42
43
        event(new UserCreatedThroughBack($user));
44
45
        return redirect()->action('Back\MembersController@index');
46
    }
47
48
    public function edit($id)
49
    {
50
        $user = User::findOrFail($id);
51
52
        return view('back.members.edit')->with(compact('user'));
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53
    }
54
55
    public function update($id, FrontUserRequest $request)
56
    {
57
        $user = User::findOrFail($id);
58
59
        $user->email = $request->get('email');
60
        $user->first_name = $request->get('first_name');
61
        $user->last_name = $request->get('last_name');
62
        $user->locale = $request->get('locale', 'nl');
63
64
        $user->save();
65
66
        $eventDescription = $this->getEventDescriptionFor('updated', $user);
67
        activity()->on($user)->log($eventDescription);
68
        flash()->success(strip_tags($eventDescription));
69
70
        return redirect()->action('Back\MembersController@index');
71
    }
72
73 View Code Duplication
    public function destroy($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $user = User::findOrFail($id);
76
77
        $user->delete();
78
79
        $eventDescription = $this->getEventDescriptionFor('deleted', $user);
80
        activity()->log($eventDescription);
81
        flash()->success(strip_tags($eventDescription));
82
83
        return redirect()->action('Back\MembersController@index');
84
    }
85
86 View Code Duplication
    protected function getEventDescriptionFor(string $event, User $user): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $name = sprintf(
89
            '<a href="%s">%s</a>',
90
            action('Back\MembersController@edit', [$user->id]),
91
            $user->email
92
        );
93
94
        if ($event === 'deleted') {
95
            $name = $user->email;
96
        }
97
98
        return trans("back.events.$event", ['model' => fragment('back.members.member'), 'name' => $name]);
99
    }
100
}
101