Completed
Push — master ( a9fd8a...71fa65 )
by Derek Stephen
10:27
created

UserController::activateAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Controller;
4
5
use App\Form\User\RegistrationForm;
6
use Del\Common\ContainerService;
7
use Del\Exception\UserException;
8
use Del\Service\UserService;
9
use Exception;
10
use OAuth\User;
11
12
class UserController extends BaseController
13
{
14
    /** @var UserService */
15
    private $userService;
16
17
    public function init()
18
    {
19
        $c = ContainerService::getInstance()->getContainer();
20
        $this->userService = $c['service.user'];
21
    }
22
23
    /**
24
     * Fetch user details
25
     * @SWG\Get(
26
     *     path="/user/{id}",
27
     *     tags={"users"},
28
     *     @SWG\Parameter(
29
     *         name="id",
30
     *         in="path",
31
     *         type="integer",
32
     *         description="the type of response",
33
     *         required=false,
34
     *         default=1
35
     *     ),
36
     *     @SWG\Response(response="200", description="Sends user details")
37
     * )
38
     *
39
     */
40
    public function indexAction()
41
    {
42
        $id = $this->getParam('id');
43
        /** @var UserService $userSvc */
44
        $userSvc = ContainerService::getInstance()->getContainer()['service.user'];
45
        /** @var User $user */
46
        $user = $userSvc->findUserById($id);
47
        if (!$user) {
48
            $this->sendJsonResponse(['User not found']);
49
        }
50
        $this->sendJsonResponse(['email' => $user->getEmail()]);
51
    }
52
53
    /**
54
     * Register as a new user.
55
     * @SWG\Post(
56
     *     path="/user/register",
57
     *     tags={"users"},
58
     *     @SWG\Response(response="200", description="Registers a new unactivated user"),
59
     *     @SWG\Parameter(
60
     *         name="email",
61
     *         in="formData",
62
     *         type="string",
63
     *         description="the users email",
64
     *         required=true,
65
     *         default="[email protected]"
66
     *     ),
67
     *     @SWG\Parameter(
68
     *         name="password",
69
     *         in="formData",
70
     *         type="string",
71
     *         description="a password for the user",
72
     *         required=true,
73
     *         default="password"
74
     *     ),
75
     *     @SWG\Parameter(
76
     *         name="confirm",
77
     *         in="formData",
78
     *         type="string",
79
     *         description="password confirmation",
80
     *         required=true,
81
     *         default="password"
82
     *     )
83
     * )
84
     * @throws Exception
85
     */
86
    public function registerAction()
87
    {
88
        $form = new RegistrationForm('register');
89
90
        if ($this->getRequest()->getMethod() == 'POST') {
91
92
            $formData = $this->getRequest()->getParsedBody();
93
            $form->populate($formData);
0 ignored issues
show
Bug introduced by
It seems like $formData defined by $this->getRequest()->getParsedBody() on line 92 can also be of type null or object; however, Del\Form\AbstractForm::populate() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
94
95
            try {
96
                $data = $form->getValues();
97
                $user = $this->userService->registerUser($data);
98
                $link = $this->userService->generateEmailLink($user);
99
                $this->sendJsonResponse([
100
                    'user' => $this->userService->toArray($user),
101
                    'link' => [
102
                        'token' => $link->getToken(),
103
                        'expires' => $link->getExpiryDate()->format('Y-m-d H:i:s'),
104
                    ],
105
                ]);
106
107
            } catch (UserException $e) {
108
109
                switch ($e->getMessage()) {
110
                    case UserException::USER_EXISTS:
111
                        throw new Exception($e->getMessage(), 400);
112
                        break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
113
                    case UserException::WRONG_PASSWORD:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114
115
                        throw new Exception($e->getMessage(), 400);
116
                        break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
117
                }
118
                throw $e;
119
            }
120
121
            $form->populate($formData);
0 ignored issues
show
Bug introduced by
It seems like $formData defined by $this->getRequest()->getParsedBody() on line 92 can also be of type null or object; however, Del\Form\AbstractForm::populate() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
122
        }
123
    }
124
125
126
127
    /**
128
     * Activate from the email link.
129
     * @SWG\Get(
130
     *     path="/user/activate/{email}/{token}",
131
     *     tags={"users"},
132
     *     @SWG\Response(response="200", description="Registers a new unactivated user"),
133
     *     @SWG\Parameter(
134
     *         name="email",
135
     *         in="path",
136
     *         type="string",
137
     *         description="the users email",
138
     *         required=true,
139
     *         default="[email protected]"
140
     *     ),
141
     *     @SWG\Parameter(
142
     *         name="token",
143
     *         in="path",
144
     *         type="string",
145
     *         description="the email link token",
146
     *         required=true,
147
     *         default="r4nd0mT0k3n"
148
     *     )
149
     * )
150
     * @throws Exception
151
     */
152
    public function activateAction()
153
    {
154
        $email = $this->getParam('email');
155
        $token = $this->getParam('token');
156
157
        $this->sendJsonResponse([
158
            'email' => $email,
159
            'token' => $token
160
        ]);
161
    }
162
}
163