Completed
Push — master ( f65d8f...4f3de5 )
by Pavel
04:14 queued 01:54
created

UserController::actionCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 3
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace App\Controller;
3
4
use App\Model\User;
5
use App\Common\JsonException;
6
7
use App\Requests\RequestResetPasswordRequest;
8
use App\Requests\ResetPasswordRequest;
9
use App\Requests\UserCreateRequest;
10
use App\Requests\UserUpdateRequest;
11
12
use Slim\Http\Request;
13
use Slim\Http\Response;
14
15
final class UserController extends CrudController
16
{
17
    /**
18
     * @param Request  $request
19
     * @param Response $response
20
     * @param array    $args
21
     *
22
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Psr\Http\Message\ResponseInterface.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
23
     * @throws JsonException
24
     */
25
    public function actionCreate(Request $request, Response $response, $args)
26
    {
27
        $params = $request->getParsedBody();
28
29
        $this->validationRequest($params, $args['entity'], new UserCreateRequest());
30
31
        $exist = User::exist($params['data']['attributes']['email']);
32
33
        if ($exist) {
34
            throw new JsonException($args['entity'], 400, 'User already exists', 'User already exists');
35
        }
36
37
        $user = User::create($params['data']['attributes']);
38
        $user->setPassword($params['data']['attributes']['password']);
39
        $user->save();
40
41
        $result = $this->encode($request, $user);
42
43
        return $this->renderer->jsonApiRender($response, 200, $result);
44
    }
45
46
    /**
47
     * @param Request  $request
48
     * @param Response $response
49
     * @param array    $args
50
     *
51
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Psr\Http\Message\ResponseInterface.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
52
     * @throws JsonException
53
     */
54
    public function actionUpdate(Request $request, Response $response, $args)
55
    {
56
        $user = User::find($args['id']);
57
58
        if (!$user) {
59
            throw new JsonException($args['entity'], 404, 'Not found', 'Entity not found');
60
        }
61
62
        $params = $request->getParsedBody();
63
64
        $this->validationRequest($params, $args['entity'], new UserUpdateRequest());
65
66
        $user->update($params['data']['attributes']);
67
68
        if (isset($params['data']['attributes']['password'])) {
69
            $user->setPassword($params['data']['attributes']['password']);
70
            $user->save();
71
        }
72
73
        $result = $this->encode($request, $user);
74
75
        return $this->renderer->jsonApiRender($response, 200, $result);
76
    }
77
78
    /**
79
     * @param Request  $request
80
     * @param Response $response
81
     * @param array    $args
82
     *
83
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Psr\Http\Message\ResponseInterface.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
84
     * @throws JsonException
85
     */
86
    public function actionRequestResetPassword(Request $request, Response $response, $args)
87
    {
88
        $params = $request->getParsedBody();
89
90
        $this->validationRequest($params, $args['entity'], new RequestResetPasswordRequest());
91
92
        $user = User::findUserByEmail($params['data']['attributes']['email']);
93
94
        if (!$user) {
95
            throw new JsonException($args['entity'], 400, 'Bad request', 'Bad request');
96
        }
97
98
        if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
99
            $user->generatePasswordResetToken();
100
        }
101
102
        if (!$user->save()) {
103
            throw new JsonException($args['entity'], 400, 'Bad request', 'Bad request');
104
        }
105
106
        $message = \Swift_Message::newInstance('Восстановление пароля для доступа в example.com')
107
            ->setFrom(['[email protected]' => 'Почтовик example.com'])
108
            ->setTo([$user->email => $user->full_name])
109
            ->setBody($this->mailRenderer->render(
110
                '/RequestResetPassword.php',
111
                [
112
                    'host' => $this->settings['params']['host'],
113
                    'token' => $user->password_reset_token
114
                ]
115
            ), 'text/html');
116
117
        if ($this->mailer->send($message)) {
0 ignored issues
show
Documentation introduced by
$message is of type object<Swift_Mime_MimePart>, but the function expects a object<Swift_Mime_Message>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
            return $this->renderer->jsonApiRender($response, 204);
119
        };
120
121
        throw new JsonException($args['entity'], 400, 'Bad request', 'Bad request');
122
    }
123
124
    /**
125
     * @param Request  $request
126
     * @param Response $response
127
     * @param array    $args
128
     *
129
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Psr\Http\Message\ResponseInterface.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
130
     * @throws JsonException
131
     */
132
    public function actionResetPassword(Request $request, Response $response, $args)
133
    {
134
        $params = $request->getParsedBody();
135
136
        $this->validationRequest($params, $args['entity'], new ResetPasswordRequest());
137
138
        $user = User::findByPasswordResetToken($params['data']['attributes']['token']);
139
140
        if ($user) {
141
            $user->setPassword($params['data']['attributes']['password']);
142
            $user->removePasswordResetToken();
143
144
            if($user->save()){
145
                return $this->renderer->jsonApiRender($response, 204);
146
            };
147
        }
148
149
        throw new JsonException($args['entity'], 400, 'Bad request', 'Bad request');
150
    }
151
}
152