Mailer::ajaxSubmit()   B
last analyzed

Complexity

Conditions 9
Paths 11

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 11
nop 0
dl 0
loc 57
rs 7.3826
c 0
b 0
f 0

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
use CMSFactory\assetManager;
4
5
if (!defined('BASEPATH')) {
6
    exit('No direct script access allowed');
7
}
8
9
/**
10
 * Image CMS
11
 *
12
 * Класс отображения страниц по ID.
13
 */
14
class Mailer extends MY_Controller
15
{
16
17
    public $settings = [];
18
19
    public function __construct() {
20
        parent::__construct();
21
22
        $this->load->module('core');
23
        $lang = new MY_Lang();
24
        $lang->load('mailer');
25
    }
26
27
    /**
28
     * Подписка пользователей.
29
     */
30
    public function index() {
31
        $this->load->library('form_validation');
32
33
        if ($this->input->post()) {
34
35
            $this->form_validation->set_rules('user_email', lang('Your e-mail', 'mailer'), 'required|trim|valid_email');
36
37
            if ($this->form_validation->run($this) == FALSE) {
38
39
                echo $errors = validation_errors();
40
                redirect('/mailer/error/');
41
            } else {
42
43
                $email = $this->input->post('user_email');
44
45
                /** @var CI_DB_result $query */
46
                $query = $this->db->get_where('mail', ['email' => $email]);
47
                $row = $query->num_rows() > 0 ? $query->row() : false;
48
49
                if ($row && $this->input->post('add_user_mail') != 1) {
50
                    redirect('/mailer/already/');
51
52
                } elseif (!$row && $this->input->post('add_user_mail') == 1) {
53
                    redirect('/mailer/no/');
54
55
                }
56
57
                if ($this->input->post('add_user_mail') == 2) {
58
59
                    $date = date('U');
60
61
                    if ($this->dx_auth->is_email_available($email)) {
62
63
                        $this->registerUserByEmail($email);
64
                    }
65
66
                    $data = [
67
                             'email' => $email,
68
                             'date'  => $date,
69
                            ];
70
71
                    $this->db->insert('mail', $data);
72
73
                    redirect('/mailer/success/');
74
                } else {
75
                    $this->db->delete('mail', ['email' => $email]);
76
                    redirect('/mailer/cancel/');
77
                }
78
            }
79
        }
80
    }
81
82
    public function ajaxSubmit() {
83
84
        $this->load->library('form_validation');
85
        $this->form_validation->set_rules('user_email', lang('Your e-mail', 'mailer'), 'required|trim|valid_email');
86
87
        if ($this->form_validation->run($this) == FALSE) {
88
            assetManager::create()
89
                ->setData(
90
                    [
91
                     'mailer_errors' => validation_errors(),
92
                    ]
93
                )
94
                ->render('error', true);
95
        } else {
96
97
            $email = $this->input->post('user_email');
98
99
            /** @var CI_DB_result $query */
100
            $query = $this->db->get_where('mail', ['email' => $email]);
101
            $row = $query->num_rows() > 0 ? $query->row() : false;
102
103
            if ($row && $this->input->post('add_user_mail') != 1) {
104
105
                assetManager::create()->render('already', true);
106
                exit;
107
            } elseif (!$row && $this->input->post('add_user_mail') == 1) {
108
109
                assetManager::create()->render('no', true);
110
                exit;
111
            }
112
113
            if ($this->input->post('add_user_mail') == 2) {
114
115
                if ($this->dx_auth->is_email_available($email)) {
116
117
                    $this->registerUserByEmail($email);
118
                }
119
120
                $date = date('U');
121
                $data = [
122
                         'email' => $email,
123
                         'date'  => $date,
124
                        ];
125
126
                $this->db->insert('mail', $data);
127
                assetManager::create()
128
                    ->setData(
129
                        ['email' => $query]
130
                    )
131
                    ->render('success', true);
132
133
            } else {
134
                $this->db->delete('mail', ['email' => $email]);
135
                assetManager::create()->render('cancel', true);
136
            }
137
        }
138
    }
139
140
    /**
141
     * Register subscribed user by email
142
     * @param string $email - user email
143
     * @return false|null
144
     */
145
    private function registerUserByEmail($email) {
146
        if (!$email) {
147
            return FALSE;
148
        }
149
        $username = array_shift(explode('@', $email));
0 ignored issues
show
Bug introduced by
explode('@', $email) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
150
        $password = random_string('alnum', 8);
151
        $key = random_string('alnum', 5);
152
        $this->dx_auth->register($username, $password, $email, '', $key);
153
    }
154
155
    /**
156
     * @return void
157
     */
158
    public function getForm() {
159
        assetManager::create()
160
            ->render('form', true);
161
    }
162
163
    /**
164
     * @return void
165
     */
166
    public function success() {
167
        assetManager::create()
168
            ->render('success', true);
169
    }
170
171
    /**
172
     * @return void
173
     */
174
    public function already() {
175
        assetManager::create()
176
            ->render('already', true);
177
    }
178
179
    /**
180
     * @return void
181
     */
182
    public function cancel() {
183
        assetManager::create()
184
            ->render('cancel', true);
185
    }
186
187
    /**
188
     *@return void
189
     */
190
    public function no() {
191
        assetManager::create()
192
            ->render('no', true);
193
    }
194
195
    /**
196
     * @return void
197
     */
198
    public function error() {
199
        assetManager::create()
200
            ->render('error', true);
201
    }
202
203
    /**
204
     *  Create modules table in db
205
     *
206
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
207
     */
208 View Code Duplication
    public function _install() {
209
210
        $this->load->dbforge();
211
212
        $fields = [
213
                   'id'    => [
214
                               'type'           => 'INT',
215
                               'constraint'     => 11,
216
                               'auto_increment' => TRUE,
217
                              ],
218
                   'email' => [
219
                               'type'       => 'VARCHAR',
220
                               'constraint' => 255,
221
                               'default'    => NULL,
222
                              ],
223
                   'date'  => [
224
                               'type'       => 'int',
225
                               'constraint' => 15,
226
                               'default'    => NULL,
227
                              ],
228
                  ];
229
230
        $this->dbforge->add_key('id', TRUE);
231
        $this->dbforge->add_field($fields);
232
        $this->dbforge->create_table('mail', TRUE);
233
234
        $this->db->where('name', 'mailer')
235
            ->update('components', ['autoload' => '1', 'enabled' => '1']);
236
    }
237
238
    public function _deinstall() {
239
240
        $this->load->dbforge();
241
        $this->dbforge->drop_table('mailer');
242
    }
243
244
}
245
246
/* End of file mailer.php */