Failed Conditions
Pull Request — master (#321)
by Anton
23:35 queued 08:33
created

modules/users/controllers/crud/post.php (15 issues)

Labels
Severity
1
<?php
2
/**
3
 * CRUD controller for POST method
4
 *
5
 * @category Application
6
 *
7
 * @author   Anton Shevchuk
8
 * @created  19.02.15 16:27
9
 */
10
11
namespace Application;
12
13
use Application\Users\Mail;
14
use Application\Users\Table;
15
use Bluz\Common\Exception\CommonException;
0 ignored issues
show
The type Bluz\Common\Exception\CommonException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Bluz\Common\Exception\ComponentException;
0 ignored issues
show
The type Bluz\Common\Exception\ComponentException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Bluz\Controller\ControllerException;
0 ignored issues
show
The type Bluz\Controller\ControllerException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Bluz\Db\Exception\DbException;
0 ignored issues
show
The type Bluz\Db\Exception\DbException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Bluz\Db\Exception\InvalidPrimaryKeyException;
0 ignored issues
show
The type Bluz\Db\Exception\InvalidPrimaryKeyException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Bluz\Db\Exception\TableNotFoundException;
0 ignored issues
show
The type Bluz\Db\Exception\TableNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Bluz\Http\Exception\ForbiddenException;
0 ignored issues
show
The type Bluz\Http\Exception\ForbiddenException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Bluz\Http\Exception\NotAcceptableException;
0 ignored issues
show
The type Bluz\Http\Exception\NotAcceptableException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Bluz\Http\Exception\NotAllowedException;
0 ignored issues
show
The type Bluz\Http\Exception\NotAllowedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Bluz\Http\Exception\NotFoundException;
0 ignored issues
show
The type Bluz\Http\Exception\NotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Bluz\Http\RequestMethod;
0 ignored issues
show
The type Bluz\Http\RequestMethod was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Bluz\Proxy\Messages;
0 ignored issues
show
The type Bluz\Proxy\Messages was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Bluz\Proxy\Request;
0 ignored issues
show
The type Bluz\Proxy\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Bluz\Validator\Exception\ValidatorException;
0 ignored issues
show
The type Bluz\Validator\Exception\ValidatorException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
30
/**
31
 * @accept HTML
32
 * @accept JSON
33
 * @method POST
34
 *
35
 * @param \Bluz\Crud\Table $crud
36
 * @param mixed $primary
37
 * @param array $data
38
 *
39
 * @return array
40
 * @throws Exception
41
 * @throws CommonException
42
 * @throws ComponentException
43
 * @throws ControllerException
44
 * @throws DbException
45
 * @throws InvalidPrimaryKeyException
46
 * @throws TableNotFoundException
47
 * @throws ForbiddenException
48
 * @throws NotAcceptableException
49
 * @throws NotAllowedException
50
 * @throws NotFoundException
51
 */
52
return function ($crud, $primary, $data) {
53
    try {
54
        // unset id
55
        unset($data['id']);
56
57
        // password
58
        $password = $data['password'] ?? null;
59
        $password2 = $data['password2'] ?? null;
60
61
        if (empty($password)) {
62
            $exception = new ValidatorException();
63
            $exception->setErrors(['password' => __('Password can\'t be empty')]);
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

63
            $exception->setErrors(['password' => /** @scrutinizer ignore-call */ __('Password can\'t be empty')]);
Loading history...
64
            throw $exception;
65
        }
66
67
        if ($password !== $password2) {
68
            $exception = new ValidatorException();
69
            $exception->setErrors(['password2' => __('Password is not equal')]);
70
            throw $exception;
71
        }
72
73
        unset($data['password2']);
74
75
        // status
76
        $data['status'] = Table::STATUS_PENDING;
77
78
        // Result is Primary Key(s)
79
        $result = $crud->createOne($data);
80
81
        $user = Table::findRow($result);
82
83
        // create auth
84
        Auth\Provider\Equals::create($user, $password);
85
86
        // send email
87
        if (Mail::activation($user, $password)) {
88
            Messages::addSuccess(
89
                'Your account has been created and an activation link has ' .
90
                'been sent to the e-mail address you entered.<br/>' .
91 1
                'Note that you must activate the account by clicking on the activation link ' .
92
                'when you get the e-mail before you can login.'
93
            );
94
        } else {
95
            Messages::addError('Unable to send email. Please contact administrator');
96
        }
97
98
        return [
99
            'row' => $user,
100
            'method' => RequestMethod::PUT
101
        ];
102
    } catch (ValidatorException $e) {
103
        $row = $crud->readOne(null);
104
        $row->setFromArray($data);
105
106
        return [
107
            'row' => $row,
108
            'errors' => $e->getErrors(),
109
            'method' => Request::getMethod()
110
        ];
111
    }
112
};
113