Completed
Pull Request — master (#252)
by
unknown
07:47
created

Crud   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 89
ccs 0
cts 47
cp 0
rs 9.1666
wmc 7
lcom 0
cbo 15

1 Method

Rating   Name   Duplication   Size   Complexity  
C createOne() 0 80 7
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