Passed
Pull Request — master (#50)
by Ronan
09:06
created

InvitationController::accept()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 41
rs 8.5866
cc 7
nc 5
nop 3
1
<?php
2
3
namespace App\Controller\Users;
4
5
use App\Facades\Mail;
6
use App\Facades\Router;
7
use App\Facades\Security;
8
use App\Facades\Session;
9
use App\Facades\View;
10
use App\Mail\User\InvitationMail;
11
use App\Model\User;
12
use Exception;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Ronanchilvers\Orm\Orm;
16
use RuntimeException;
17
18
/**
19
 * Controller for user invitations
20
 *
21
 * @author Ronan Chilvers <[email protected]>
22
 */
23
class InvitationController
24
{
25
    /**
26
     * Invite a user
27
     *
28
     * @author Ronan Chilvers <[email protected]>
29
     */
30
    public function create(
31
        ServerRequestInterface $request,
32
        ResponseInterface $response,
33
        $args
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

33
        /** @scrutinizer ignore-unused */ $args

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    ) {
35
        $user = new User();
36
        if ('POST' == $request->getMethod()) {
37
            $data = $request->getParsedBody()['user'];
38
            $user->fromArray($data);
39
            if ($user->saveWithValidation('invitation')) {
40
                Session::flash([
41
                    'heading' => 'Invitation created'
42
                ]);
43
                Mail::send(
0 ignored issues
show
Bug introduced by
The method send() does not exist on App\Facades\Mail. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
                Mail::/** @scrutinizer ignore-call */ 
44
                      send(
Loading history...
44
                    new InvitationMail($user)
45
                );
46
                return $response->withRedirect(
0 ignored issues
show
Bug introduced by
The method withRedirect() does not exist on Psr\Http\Message\ResponseInterface. It seems like you code against a sub-type of Psr\Http\Message\ResponseInterface such as Slim\Http\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
                return $response->/** @scrutinizer ignore-call */ withRedirect(
Loading history...
47
                    Router::pathFor('users.index')
48
                );
49
            }
50
        }
51
52
        return View::render(
53
            $response,
54
            '@web/users/invitations/create.html.twig',
55
            [
56
                'user' => $user,
57
            ]
58
        );
59
    }
60
61
    /**
62
     * Action to accept an invitation
63
     *
64
     * @author Ronan Chilvers <[email protected]>
65
     */
66
    public function accept(
67
        ServerRequestInterface $request,
68
        ResponseInterface $response,
69
        $args
70
    ) {
71
        if (!isset($args['hash']) || 0 == strlen($args['hash'])) {
72
            return $response->withRedirect(
73
                Router::pathFor('user.login')
74
            );
75
        }
76
        $hash = filter_var($args['hash'], FILTER_SANITIZE_STRING);
0 ignored issues
show
Unused Code introduced by
The assignment to $hash is dead and can be removed.
Loading history...
77
        $user = Orm::finder(User::class)->forHash($args['hash']);
78
        if (!$user instanceof User) {
79
            return $response->withRedirect(
80
                Router::pathFor('user.login')
81
            );
82
        }
83
        if ('POST' == $request->getMethod()) {
84
            $data     = $request->getParsedBody()['user'];
85
            $password = $request->getParsedBody()['password'];
86
            $user->fromArray($data);
87
            $user->setPassword(
88
                $password['value'],
89
                $password['confirm'],
90
            );
91
            $user->activate();
92
            if ($user->validate() && $user->save()) {
93
                Session::flash([
94
                    'heading' => 'Saved'
95
                ]);
96
                return $response->withRedirect(
97
                    Router::pathFor('users.index')
98
                );
99
            }
100
        }
101
102
        return View::render(
103
            $response,
104
            '@web/users/invitations/accept.html.twig',
105
            [
106
                'user' => $user,
107
            ]
108
        );
109
    }
110
}
111