UserInvitationsController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 182
rs 10
wmc 22
lcom 2
cbo 8

12 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 10 1
A show() 0 5 1
A update() 0 6 1
A accept() 0 6 3
A postAccept() 0 19 3
B validateToken() 0 15 5
A showAcceptUserInvitationForm() 0 8 1
A userInvitations() 0 5 1
A invite() 0 5 2
A sendInvitation() 0 4 1
A store() 0 12 2
A index() 0 5 1
1
<?php
2
3
namespace Acacha\Users\Http\Controllers;
4
5
use Acacha\Users\Events\UserCreated;
6
use Acacha\Users\Events\UserInvited;
7
use Acacha\Users\Http\Requests\CreateUserWithTokenRequest;
8
use Acacha\Users\Http\Requests\SendInvitationRequest;
9
use Acacha\Users\Http\Requests\UpdateInvitationRequest;
10
use Acacha\Users\Models\UserInvitation;
11
use App\User;
12
use Illuminate\Database\Eloquent\ModelNotFoundException;
13
use Illuminate\Http\Request;
14
use Response;
15
16
/**
17
 * Class UserInvitationsController
18
 *
19
 * @package Acacha\Users\Http\Controllers
20
 */
21
class UserInvitationsController extends Controller
22
{
23
    /**
24
     * Send Invitation.
25
     */
26
    public function sendInvitation(SendInvitationRequest $request)
27
    {
28
        return $this->store($request);
29
    }
30
31
    /**
32
     * Store a newly created resource in storage.
33
     *
34
     * @param SendInvitationRequest $request
35
     * @return \Illuminate\Http\JsonResponse
36
     */
37
    public function store(SendInvitationRequest $request)
38
    {
39
        $invitation = UserInvitation::where(['email' => $request->input('email')])->first();
40
        if (!$invitation) {
41
            $invitation = UserInvitation::create($request->only(['email','state','token']));
0 ignored issues
show
Bug introduced by
The method create() does not exist on Acacha\Users\Models\UserInvitation. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
42
        }
43
44
        // NOTE : this method trigger method "created" in UserInvitationObserver. Fire also and event to enable hooking.
45
        event(new UserInvited($invitation));
46
47
        return Response::json(['created' => true ]);
48
    }
49
50
    /**
51
     * Remove the specified resource from storage.
52
     *
53
     * @param  int  $id
54
     * @return \Illuminate\Http\Response
55
     */
56
    public function destroy($id)
57
    {
58
        $this->authorize('delete-user-invitations');
59
        UserInvitation::destroy($id);
60
61
        // NOTE : this method trigger method "created" in UserInvitationObserver. Fire also and event to enable hooking.
62
//        event(new UserInvited($invitation));
63
64
        return Response::json(['deleted' => true ]);
65
    }
66
67
    /**
68
     * Display a listing of the resource.
69
     *
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function index()
73
    {
74
        $this->authorize('list-user-invitations');
75
        return UserInvitation::paginate();
76
    }
77
78
    /**
79
     * Display the specified resource.
80
     *
81
     * @param  int  $id
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function show($id)
85
    {
86
        $this->authorize('view-user-invitations');
87
        return UserInvitation::find($id);
88
    }
89
90
    /**
91
     * Update the specified resource in storage.
92
     *
93
     * @param UpdateInvitationRequest $request
94
     * @param $id
95
     * @return \Illuminate\Http\JsonResponse
96
     */
97
    public function update(UpdateInvitationRequest $request, $id)
98
    {
99
        $invitation = UserInvitation::find($id);
100
        $invitation->update($request->intersect(['email','state','token']));
0 ignored issues
show
Documentation Bug introduced by
The method intersect does not exist on object<Acacha\Users\Http...pdateInvitationRequest>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
101
        return Response::json(['updated' => true ]);
102
    }
103
104
    /**
105
     * Accept user invitation.
106
     *
107
     * @param Request $request
108
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
109
     */
110
    public function accept(Request $request)
111
    {
112
        if (! $request->has('token')) abort(404);
113
        if (! $invitation = $this->validateToken($request->input('token'))) abort(404);
114
        return $this->showAcceptUserInvitationForm($invitation);
115
    }
116
117
    /**
118
     * Process accept user invitation form.
119
     *
120
     * @param CreateUserWithTokenRequest $request
121
     * @return \Illuminate\Http\JsonResponse
122
     */
123
    public function postAccept(CreateUserWithTokenRequest $request)
124
    {
125
        if (! $request->has('token')) abort(403);
126
        if (! $invitation = $this->validateToken($request->input('token'))) abort(403);
127
128
        $user = User::create([
129
            'name' => $request->input('name'),
130
            'email' => $request->input('email'),
131
            'password' => bcrypt($request->input('password')),
0 ignored issues
show
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, bcrypt() 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...
132
        ]);
133
134
        event(new UserCreated($user));
135
136
        $invitation = UserInvitation::where('token', $request->input('token'))->first();
137
        $invitation->user()->associate($user);
138
        $invitation->accept();
139
140
        return Response::json(['created' => true ]);
141
    }
142
143
    /**
144
     * Validate token.
145
     *
146
     * @param $token
147
     * @return bool
148
     */
149
    protected function validateToken($token)
150
    {
151
        if (!$token) return false;
152
        try {
153
            $invitation = UserInvitation::where('token', $token)
154
                ->where('state', 'pending')->first();
155
            if ($invitation) {
156
                if ( $invitation->token === $token) return $invitation;
157
            }
158
        } catch (ModelNotFoundException $e) {
159
            return false;
160
        }
161
162
        return false;
163
    }
164
165
    /**
166
     * Show accept user invitation form.
167
     *
168
     * @param $invitation
169
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
170
     */
171
    protected function showAcceptUserInvitationForm($invitation)
172
    {
173
        $data = [
174
            'email' => $invitation->email,
175
            'token' => $invitation->token
176
        ];
177
        return view('acacha_users::accept-invitation',$data);
178
    }
179
180
    /**
181
     * Show the management user invitations page.
182
     *
183
     * @return Response
184
     */
185
    public function userInvitations()
186
    {
187
        $this->authorize('see-manage-user-invitations-view');
188
        return view('acacha_users::management-invitations');
189
    }
190
191
    /**
192
     * Show the user invitations public page.
193
     *
194
     * @return Response
195
     */
196
    public function invite()
197
    {
198
        if (!config('users.users_can_invite_other_users')) abort(404);
199
        return view('acacha_users::invite-user');
200
    }
201
202
}