Completed
Push — master ( 30a636...983987 )
by
unknown
15s
created

Crud::createOne()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 80
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 6
Bugs 1 Features 0
Metric Value
cc 7
eloc 45
c 6
b 1
f 0
nc 48
nop 1
dl 0
loc 80
ccs 0
cts 47
cp 0
crap 56
rs 6.5597

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
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Users;
11
12
use Application\Auth;
13
use Application\Exception;
14
use Application\UsersActions;
15
use Bluz\Application\Application;
16
use Bluz\Proxy\Logger;
17
use Bluz\Proxy\Mailer;
18
use Bluz\Proxy\Messages;
19
use Bluz\Proxy\Router;
20
use Bluz\Validator\Exception\ValidatorException;
21
22
/**
23
 * Crud
24
 *
25
 * @package  Application\Users
26
 *
27
 * @method   Table getTable()
28
 *
29
 * @author   Anton Shevchuk
30
 * @created  30.10.12 16:11
31
 */
32
class Crud extends \Bluz\Crud\Table
33
{
34
    /**
35
     * @param array $data
36
     * @throws Exception
37
     * @throws ValidatorException
38
     * @return integer
39
     */
40
    public function createOne($data)
41
    {
42
        // password
43
        $password = isset($data['password']) ? $data['password'] : null;
44
        $password2 = isset($data['password2']) ? $data['password2'] : null;
45
46
        if (empty($password)) {
47
            throw ValidatorException::exception('password', __('Password can\'t be empty'));
48
        }
49
50
        if ($password !== $password2) {
51
            throw ValidatorException::exception('password2', __('Password is not equal'));
52
        }
53
54
        if ($data['id'] == '') {
55
            unset($data['id']);
56
        }
57
58
        /** @var $row Row */
59
        $row = $this->getTable()->create();
60
        $row->setFromArray($data);
61
        $row->status = Table::STATUS_PENDING;
62
        $row->save();
63
64
        $userId = $row->id;
65
66
        // create auth
67
        Auth\Table::getInstance()->generateEquals($row, $password);
68
69
        // create activation token
70
        // valid for 5 days
71
        $actionRow = UsersActions\Table::getInstance()->generate($userId, UsersActions\Table::ACTION_ACTIVATION, 5);
72
73
        // send activation email
74
        // generate activation URL
75
        $activationUrl = Router::getFullUrl(
76
            'users',
77
            'activation',
78
            ['code' => $actionRow->code, 'id' => $userId]
79
        );
80
81
        $subject = "Activation";
82
83
        $body = Application::getInstance()->dispatch(
84
            'users',
85
            'mail/template',
86
            [
87
                'template' => 'registration',
88
                'vars' => ['user' => $row, 'activationUrl' => $activationUrl, 'password' => $password]
89
            ]
90
        )->render();
91
92
        try {
93
            $mail = Mailer::create();
94
            $mail->Subject = $subject;
95
            $mail->msgHTML(nl2br($body));
96
            $mail->addAddress($data['email']);
97
            Mailer::send($mail);
98
        } catch (\Exception $e) {
99
            Logger::log(
100
                'error',
101
                $e->getMessage(),
102
                ['module' => 'users', 'controller' => 'change-email', 'userId' => $userId]
103
            );
104
105
            throw new Exception('Unable to send email. Please contact administrator.');
106
        }
107
108
        // show notification and redirect
109
        Messages::addSuccess(
0 ignored issues
show
Bug introduced by
The call to addSuccess() misses a required argument $...$text.

This check looks for function calls that miss required arguments.

Loading history...
110
            "Your account has been created and an activation link has" .
111
            "been sent to the e-mail address you entered.<br/>" .
112
            "Note that you must activate the account by clicking on the activation link" .
113
            "when you get the e-mail before you can login."
114
        );
115
        // wtf?
116
        // redirectTo('index', 'index');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
118
        return $userId;
119
    }
120
}
121