Completed
Pull Request — master (#24)
by Christopher
08:17
created

PendingUserController::getSignUpForm()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 34
c 0
b 0
f 0
nc 5
nop 3
dl 0
loc 56
rs 8.7592

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TechWilk\Rota\Controller;
4
5
use Exception;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use TechWilk\Rota\EmailAddress;
9
use TechWilk\Rota\PendingUser;
10
use TechWilk\Rota\PendingUserQuery;
11
12
class PendingUserController extends BaseController
13
{
14
    public function getSignUpForm(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

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

Loading history...
Coding Style introduced by
getSignUpForm uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
15
    {
16
        $this->logger->info("Fetch sign-up GET '/signup'");
17
18
        if (isset($_SESSION['userId'])) {
19
            return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('home'));
20
        }
21
22
        $auth = $this->auth;
23
        $slug = $auth->getAuthProviderSlug();
24
25
        try {
26
            $auth->getSocialUserId();
27
        } catch (Exception $e) {
28
            return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('login'));
29
        }
30
31
        $existingPendingUser = PendingUserQuery::create()
32
            ->filterBySocialId($auth->getSocialUserId())
33
            ->filterBySource($slug)
34
            ->findOne();
35
36
        if (!is_null($existingPendingUser)) {
37
            $firstName = $existingPendingUser->getFirstName();
38
39
            // remove any fb auth tokens
40
            session_unset();
41
42
            return $this->view->render($response, 'login-sign-up-complete.twig', [
43
                'firstname' => $firstName,
44
            ]);
45
        }
46
47
        $firstName = '';
48
        $lastName = '';
49
        $email = '';
50
51
        switch ($slug) {
52
            case 'facebook':
53
                $meta = $auth->getMeta();
54
55
                // Split first and last names from FB
56
                $names = explode(' ', $meta['name'], 2);
57
58
                $firstName = $names[0];
59
                $lastName = $names[1];
60
                $email = $meta['email'];
61
            break;
62
        }
63
64
        return $this->view->render($response, 'login-sign-up.twig', [
65
            'firstname' => $firstName,
66
            'lastname'  => $lastName,
67
            'email'     => $email,
68
        ]);
69
    }
70
71
    public function postSignUp(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

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

Loading history...
Coding Style introduced by
postSignUp uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
72
    {
73
        $this->logger->info("Submit sign-up POST '/signup'");
74
75
        if (isset($_SESSION['userId'])) {
76
            return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('home'));
77
        }
78
79
        $auth = $this->auth;
80
        $data = $request->getParsedBody();
81
82
        $firstName = filter_var(trim($data['firstName']), FILTER_SANITIZE_STRING);
83
        $lastName = filter_var(trim($data['lastName']), FILTER_SANITIZE_STRING);
84
        $email = new EmailAddress(trim($data['email']));
85
86
        $pendingUser = new PendingUser();
87
        $pendingUser->setSocialId($auth->getSocialUserId());
88
        $pendingUser->setFirstName($firstName);
89
        $pendingUser->setLastName($lastName);
90
        $pendingUser->setEmail($email);
91
        $pendingUser->setSource($auth->getAuthProviderSlug());
92
        $pendingUser->save();
93
94
        return $this->view->render($response, 'login-sign-up-complete.twig', [
95
            'firstname' => $firstName,
96
        ]);
97
    }
98
99
    public function getSignUpCancel(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

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

Loading history...
100
    {
101
        session_unset();
102
103
        return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('login'));
104
    }
105
}
106