Completed
Push — development ( ef9e73...b2c3e4 )
by Andrij
20:27
created

Found_less_expensive   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 157
Duplicated Lines 3.82 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 6
loc 157
rs 10
wmc 11
lcom 0
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A adminAutoload() 0 3 1
A showButtonWithForm() 0 6 1
B save_message() 6 24 3
A sendEmail() 0 14 1
A _install() 0 69 2
A _deinstall() 0 5 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
use cmsemail\email;
4
use CMSFactory\assetManager;
5
6
if (!defined('BASEPATH')) {
7
    exit('No direct script access allowed');
8
}
9
10
/**
11
 * @property Found_less_expensive_model found_less_expensive_model
12
 * @author Igor R.
13
 * @copyright ImageCMS (c) 2013, Igor R. <[email protected]>
14
 *
15
 * In order to render button and link insert into product template
16
 * {$CI->load->module('found_less_expensive')->showButtonWithForm()}
17
 *
18
 * Нашли дешевле
19
 */
20
class Found_less_expensive extends MY_Controller
21
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
22
23
    public function __construct() {
24
25
        parent::__construct();
26
        $this->load->model('found_less_expensive_model');
27
        $this->load->library('email');
28
        $lang = new MY_Lang();
29
        $lang->load('found_less_expensive');
30
    }
31
32
    public static function adminAutoload() {
33
        parent::adminAutoload();
34
    }
35
36
    /**
37
     * Display button and form
38
     */
39
    public function showButtonWithForm() {
40
        assetManager::create()
41
                ->registerStyle('style')
42
                ->registerScript('scripts')
43
                ->render('buttonWithForm', true);
44
    }
45
46
    /**
47
     * @return array|string
48
     */
49
    public function save_message() {
50
51
        $this->load->library('from_validation');
52
        $this->form_validation->set_rules('name', lang('Name', 'admin'), 'required');
53
        $this->form_validation->set_rules('phone', lang('Phone', 'admin'), 'required|numeric');
54
        $this->form_validation->set_rules('email', lang('Email', 'admin'), 'required|valid_email');
55
        $this->form_validation->set_rules('link', lang('link', 'admin'), 'required');
56
57 View Code Duplication
        if (!$this->form_validation->run()) {
58
            return [
59
                    'message' => validation_errors(),
60
                    'errors'  => $this->form_validation->getErrorsArray(),
61
                   ];
62
        }
63
64
        $data = $this->input->post();
65
        $data['date'] = time();
66
        $data['status'] = 0;
67
        unset($data['cms_token']);
68
        if ($this->db->insert('mod_found_less_expensive', $data)) {
69
            $this->sendEmail($data);
70
            return 'success';
71
        }
72
    }
73
74
    /**
75
     * Send email
76
     * @param array $messageData
77
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
78
     */
79
    public function sendEmail(array $messageData) {
80
81
        $variables = [
82
                      'userName'    => $messageData['name'],
83
                      'userEmail'   => $messageData['email'],
84
                      'userPhone'   => $messageData['phone'],
85
                      'userLink'    => $messageData['link'],
86
                      'userMessage' => $messageData['question'],
87
                      'date'        => date('d-m-Y H:i:s', $messageData['date']),
88
                      'productUrl'  => $messageData['productUrl'],
89
90
                     ];
91
        return email::getInstance()->sendEmail($this->dx_auth->get_user_email(), 'Found_less_expensive', $variables);
92
    }
93
94
    /**
95
     * Install module
96
     */
97
    public function _install() {
98
99
        $email_patterns = $this->found_less_expensive_model->getEmailPatterns();
100
        $this->db->insert('mod_email_paterns', $email_patterns);
101
102
        $email_patterns_i18n = $this->found_less_expensive_model->getEmailPatternsI18n();
103
        $this->db->insert('mod_email_paterns_i18n', $email_patterns_i18n);
104
105
        $this->load->dbforge();
106
        ($this->dx_auth->is_admin()) OR exit;
107
        $fields = [
108
                   'id'         => [
109
                                    'type'           => 'INT',
110
                                    'auto_increment' => TRUE,
111
                                   ],
112
                   'name'       => [
113
                                    'type'       => 'VARCHAR',
114
                                    'constraint' => '70',
115
                                    'null'       => TRUE,
116
                                   ],
117
                   'email'      => [
118
                                    'type'       => 'VARCHAR',
119
                                    'constraint' => '50',
120
                                    'null'       => TRUE,
121
                                   ],
122
                   'phone'      => [
123
                                    'type'       => 'VARCHAR',
124
                                    'constraint' => '50',
125
                                    'null'       => TRUE,
126
                                   ],
127
                   'question'   => [
128
                                    'type'       => 'VARCHAR',
129
                                    'constraint' => '250',
130
                                    'null'       => TRUE,
131
                                   ],
132
                   'link'       => [
133
                                    'type'       => 'VARCHAR',
134
                                    'constraint' => '150',
135
                                    'null'       => TRUE,
136
                                   ],
137
                   'productUrl' => [
138
                                    'type'       => 'VARCHAR',
139
                                    'constraint' => '250',
140
                                    'null'       => TRUE,
141
                                   ],
142
                   'date'       => [
143
                                    'type' => 'INT',
144
                                    'null' => TRUE,
145
                                   ],
146
                   'status'     => [
147
                                    'type'       => 'VARCHAR',
148
                                    'constraint' => '150',
149
                                    'null'       => TRUE,
150
                                   ],
151
                  ];
152
153
        $this->dbforge->add_field($fields);
154
        $this->dbforge->add_key('id', TRUE);
155
        $this->dbforge->create_table('mod_found_less_expensive');
156
157
        $this->db->where('name', 'found_less_expensive');
158
        $this->db->update(
159
            'components',
160
            [
161
             'enabled'  => 1,
162
             'autoload' => 1,
163
            ]
164
        );
165
    }
166
167
    /**
168
     * Deinstall module
169
     */
170
    public function _deinstall() {
171
        $this->load->dbforge();
172
        ($this->dx_auth->is_admin()) OR exit;
173
        $this->dbforge->drop_table('mod_found_less_expensive');
174
    }
175
176
}