Passed
Pull Request — master (#50)
by Ronan
10:11
created

InvitationController::accept()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 41
rs 8.5866
c 1
b 0
f 0
ccs 0
cts 17
cp 0
cc 7
nc 5
nop 3
crap 56
1
<?php
2
3
namespace App\Controller\Users;
4
5
use App\Facades\Router;
6
use App\Facades\Security;
7
use App\Facades\Session;
8
use App\Facades\View;
9
use App\Model\User;
10
use Exception;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Ronanchilvers\Orm\Orm;
14
use RuntimeException;
15
16
/**
17
 * Controller for user invitations
18
 *
19
 * @author Ronan Chilvers <[email protected]>
20
 */
21
class InvitationController
22
{
23
    /**
24
     * Invite a user
25
     *
26
     * @author Ronan Chilvers <[email protected]>
27
     */
28
    public function create(
29
        ServerRequestInterface $request,
30
        ResponseInterface $response,
31
        $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

31
        /** @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...
32
    ) {
33
        $user = new User();
34
        if ('POST' == $request->getMethod()) {
35
            $data = $request->getParsedBody()['user'];
36
            $user->fromArray($data);
37
            if ($user->saveWithValidation('invitation')) {
38
                Session::flash([
39
                    'heading' => 'Invitation created'
40
                ]);
41
                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

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