Completed
Push — master ( 7ef4f0...fad325 )
by Sebastian
17:53 queued 12:31
created

AdministratorsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Back;
4
5
use App\Services\Auth\Back\Enums\UserRole;
6
use App\Services\Auth\Back\Enums\UserStatus;
7
use App\Services\Auth\Back\Events\UserCreated;
8
use App\Http\Requests\Back\BackUserRequest;
9
use App\Services\Auth\Back\User;
10
use App\Services\Auth\Back\UserUpdater;
11
use App\Repositories\BackUserRepository;
12
13
class AdministratorsController
14
{
15
    /** @var \App\Repositories\BackUserRepository */
16
    protected $backUserRepository;
17
18
    public function __construct(BackUserRepository $backUserRepository)
19
    {
20
        $this->backUserRepository = $backUserRepository;
21
    }
22
23
    public function index()
24
    {
25
        $users = $this->backUserRepository->getAll();
26
27
        return view('back.administrators.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...
28
    }
29
30
    public function create()
31
    {
32
        return view('back.administrators.create', ['user' => new User()]);
33
    }
34
35
    public function store(BackUserRequest $request)
36
    {
37
        $user = new User();
38
39
        $user->email = $request->get('email');
40
        $user->first_name = $request->get('first_name');
41
        $user->last_name = $request->get('last_name');
42
        $user->locale = $request->get('locale', 'nl');
43
44
        if ($request->has('password')) {
45
            $user->password = $request->get('password');
46
        }
47
48
        $user->role = UserRole::ADMIN();
49
        $user->status = UserStatus::ACTIVE();
50
51
        $this->backUserRepository->save($user);
52
53
        $eventDescription = $this->getEventDescriptionFor('created', $user);
54
        activity()->on($user)->log($eventDescription);
55
        flash()->success(strip_tags($eventDescription).'. '.fragment('back.administrators.passwordMailSent'));
56
57
        event(new UserCreated($user));
58
59
        return redirect(action('Back\AdministratorsController@index', ['role' => $user->role]));
60
    }
61
62
    public function edit($id)
63
    {
64
        $user = $this->backUserRepository->find($id);
65
66
        if (!$user) {
67
            abort(404);
68
        }
69
70
        return view('back.administrators.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...
71
    }
72
73
    public function update($id, BackUserRequest $request)
74
    {
75
        abort_unless($user = $this->backUserRepository->find($id), 500);
0 ignored issues
show
Documentation introduced by
$user = $this->backUserRepository->find($id) is of type object<Illuminate\Database\Eloquent\Model>|null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77
        $user->email = $request->get('email');
78
        $user->first_name = $request->get('first_name');
79
        $user->last_name = $request->get('last_name');
80
        $user->locale = $request->get('locale', 'nl');
81
82
        if ($request->has('password')) {
83
            $user->password = $request->get('password');
84
        }
85
86
        $this->backUserRepository->save($user);
87
88
        $eventDescription = $this->getEventDescriptionFor('updated', $user);
89
        activity()->log($eventDescription);
90
        flash()->success(strip_tags($eventDescription));
91
92
        return redirect()->action('Back\AdministratorsController@index');
93
    }
94
95 View Code Duplication
    public function activate($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...
96
    {
97
        abort_unlees($user = $this->backUserRepository->find($id), 500);
98
99
        $user->activate();
100
101
        $eventDescription = $this->getEventDescriptionFor('activated', $user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->backUserRepository->find($id) on line 97 can be null; however, App\Http\Controllers\Bac...etEventDescriptionFor() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
102
        activity($eventDescription);
103
        flash()->success(strip_tags($eventDescription));
104
105
        return back();
106
    }
107
108 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...
109
    {
110
        $user = $this->backUserRepository->find($id);
111
112
        $eventDescription = $this->getEventDescriptionFor('deleted', $user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->backUserRepository->find($id) on line 110 can be null; however, App\Http\Controllers\Bac...etEventDescriptionFor() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
113
114
        $this->backUserRepository->delete($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->backUserRepository->find($id) on line 110 can be null; however, App\Repositories\BackUserRepository::delete() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
115
116
        activity($eventDescription);
117
        flash()->success(strip_tags($eventDescription));
118
119
        return redirect()->action('Back\AdministratorsController@index');
120
    }
121
122 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...
123
    {
124
        $name = sprintf(
125
            '<a href="%s">%s</a>',
126
            action('Back\AdministratorsController@edit', [$user->id]),
127
            $user->email
128
        );
129
130
        if ($event === 'deleted') {
131
            $name = $user->email;
132
        }
133
134
        return trans("back.events.$event", ['model' => fragment('back.administrators.administrator'), 'name' => $name]);
135
    }
136
}
137