Completed
Push — master ( 02f92d...9c9507 )
by Sebastian
15:38
created

BackUserController::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 19
rs 9.4285
cc 1
eloc 11
nc 1
nop 1
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\UserWasCreated;
8
use App\Http\Controllers\Controller;
9
use App\Http\Requests\Back\BackUserRequest;
10
use App\Services\Auth\Back\User;
11
use App\Services\Auth\Back\UserUpdater;
12
use App\Repositories\BackUserRepository;
13
14
class BackUserController extends Controller
15
{
16
    /** @var \App\Repositories\BackUserRepository */
17
    protected $backUserRepository;
18
19
    public function __construct(BackUserRepository $backUserRepository)
20
    {
21
        $this->backUserRepository = $backUserRepository;
22
    }
23
24
    public function index()
25
    {
26
        $users = $this->backUserRepository->getAll();
27
28
        return view('back.backUsers.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...
29
    }
30
31
    public function create()
32
    {
33
        return view('back.backUsers.create', ['user' => new User()]);
34
    }
35
36 View Code Duplication
    public function store(BackUserRequest $request)
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...
37
    {
38
        $user = new User();
39
40
        UserUpdater::update($user, $request);
41
42
        $user->role = UserRole::ADMIN();
43
        $user->status = UserStatus::ACTIVE();
44
45
        $this->backUserRepository->save($user);
46
47
        $eventDescription = $this->getEventDescriptionFor('created', $user);
48
        activity($eventDescription);
49
        flash()->success(strip_tags($eventDescription).'. '.fragment('back.backUsers.passwordMailSent'));
50
51
        event(new UserWasCreated($user));
52
53
        return redirect(action('Back\BackUserController@index', ['role' => $user->role]));
54
    }
55
56
    public function edit($id)
57
    {
58
        $user = $this->backUserRepository->find($id);
59
60
        if (! $user) abort(404);
61
62
        return view('back.backUsers.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...
63
    }
64
65 View Code Duplication
    public function update($id, BackUserRequest $request)
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...
66
    {
67
        $user = $this->backUserRepository->findOrAbort($id);
0 ignored issues
show
Bug introduced by
The method findOrAbort() does not seem to exist on object<App\Repositories\BackUserRepository>.

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...
68
69
        UserUpdater::update($user, $request);
70
71
        $this->backUserRepository->save($user);
72
73
        $eventDescription = $this->getEventDescriptionFor('updated', $user);
74
        activity($eventDescription);
75
        flash()->success(strip_tags($eventDescription));
76
77
        return redirect()->action('Back\BackUserController@index');
78
    }
79
80 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...
81
    {
82
        $user = $this->backUserRepository->findOrAbort($id);
0 ignored issues
show
Bug introduced by
The method findOrAbort() does not seem to exist on object<App\Repositories\BackUserRepository>.

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...
83
84
        $user->activate();
85
86
        $eventDescription = $this->getEventDescriptionFor('activated', $user);
87
        activity($eventDescription);
88
        flash()->success(strip_tags($eventDescription));
89
90
        return back();
91
    }
92
93 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...
94
    {
95
        $user = $this->backUserRepository->find($id);
96
97
        $eventDescription = $this->getEventDescriptionFor('deleted', $user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Illuminate\Database\Eloquent\Model>|null, but the function expects a object<App\Services\Auth\Back\User>.

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...
98
99
        $this->backUserRepository->delete($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->backUserRepository->find($id) on line 95 can be null; however, App\Foundation\Repositories\Repository::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...
100
101
        activity($eventDescription);
102
        flash()->success(strip_tags($eventDescription));
103
104
        return redirect()->action('Back\BackUserController@index');
105
    }
106
107 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...
108
    {
109
        $name = sprintf(
110
            '<a href="%s">%s</a>',
111
            action('Back\BackUserController@edit', [$user->id]),
112
            $user->email
113
        );
114
115
        if ($event === 'deleted') {
116
            $name = $user->email;
117
        }
118
119
        return trans("back.events.$event", ['model' => fragment('back.backUsers.administrator'), 'name' => $name]);
120
    }
121
}
122