Settings   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 119
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A editSettings() 0 12 1
A prepareDataEditSettings() 0 8 2
A setTitleEditSettings() 0 5 1
A setBreadcrumbEditSettings() 0 16 1
A submitSettings() 0 6 3
A validateSettings() 0 18 3
A updateSettings() 0 6 1
A outputEditSettings() 0 4 1
1
<?php
2
3
/**
4
 * @package Error Notifier
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\error_notifier\controllers;
11
12
use gplcart\core\controllers\backend\Controller as BackendController;
13
14
/**
15
 * Handles incoming requests and outputs data related to Error Notifier module
16
 */
17
class Settings extends BackendController
18
{
19
20
    /**
21
     * Constructor
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
    }
27
28
    /**
29
     * Route page callback to display the module settings page
30
     */
31
    public function editSettings()
32
    {
33
        $this->setTitleEditSettings();
34
        $this->setBreadcrumbEditSettings();
35
36
        $this->setData('settings', $this->module->getSettings('error_notifier'));
37
38
        $this->submitSettings();
39
        $this->prepareDataEditSettings();
40
41
        $this->outputEditSettings();
42
    }
43
44
    /**
45
     * Prepare data before passing to templates
46
     */
47
    protected function prepareDataEditSettings()
48
    {
49
        $recipients = $this->getData('settings.recipient');
50
51
        if (is_array($recipients)) {
52
            $this->setData('settings.recipient', implode("\n", $recipients));
53
        }
54
    }
55
56
    /**
57
     * Set title on the module settings page
58
     */
59
    protected function setTitleEditSettings()
60
    {
61
        $title = $this->text('Edit %name settings', array('%name' => $this->text('Error Notifier')));
62
        $this->setTitle($title);
63
    }
64
65
    /**
66
     * Set breadcrumbs on the module settings page
67
     */
68
    protected function setBreadcrumbEditSettings()
69
    {
70
        $breadcrumbs = array();
71
72
        $breadcrumbs[] = array(
73
            'text' => $this->text('Dashboard'),
74
            'url' => $this->url('admin')
75
        );
76
77
        $breadcrumbs[] = array(
78
            'text' => $this->text('Modules'),
79
            'url' => $this->url('admin/module/list')
80
        );
81
82
        $this->setBreadcrumbs($breadcrumbs);
83
    }
84
85
    /**
86
     * Saves the submitted settings
87
     */
88
    protected function submitSettings()
89
    {
90
        if ($this->isPosted('save') && $this->validateSettings()) {
91
            $this->updateSettings();
92
        }
93
    }
94
95
    /**
96
     * Validate submitted module settings
97
     */
98
    protected function validateSettings()
99
    {
100
        $this->setSubmitted('settings');
101
        $this->setSubmittedBool('email');
102
103
        $this->validateElement('live_limit', 'integer');
104
        $this->validateElement('email_limit', 'integer');
105
106
        foreach ($this->setSubmittedArray('recipient') as $email) {
107
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
108
                $message = $this->text('@field has invalid value', array('@field' => $this->text('Recipients')));
109
                $this->setError('recipient', $message);
110
                break;
111
            }
112
        }
113
114
        return !$this->hasErrors();
115
    }
116
117
    /**
118
     * Update module settings
119
     */
120
    protected function updateSettings()
121
    {
122
        $this->controlAccess('module_edit');
123
        $this->module->setSettings('error_notifier', $this->getSubmitted());
124
        $this->redirect('', $this->text('Settings have been updated'), 'success');
125
    }
126
127
    /**
128
     * Render and output the module settings page
129
     */
130
    protected function outputEditSettings()
131
    {
132
        $this->output('error_notifier|settings');
133
    }
134
135
}
136