Settings::outputEditSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package API
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0-or-later
8
 */
9
10
namespace gplcart\modules\api\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
14
/**
15
 * Handles incoming requests and outputs data related to API module
16
 */
17
class Settings extends Controller
18
{
19
    /**
20
     * Constructor
21
     */
22
    public function __construct()
23
    {
24
        parent::__construct();
25
    }
26
27
    /**
28
     * Route page callback to display the module settings page
29
     */
30
    public function editSettings()
31
    {
32
        $this->setTitleEditSettings();
33
        $this->setBreadcrumbEditSettings();
34
35
        $this->setData('algs', $this->getHashingAlgorithms());
36
        $this->setData('settings', $this->getModuleSettings('api'));
37
38
        $this->submitSettings();
39
        $this->outputEditSettings();
40
    }
41
42
    /**
43
     * Returns an array of supported hashing algorithms fo JWT tokens
44
     * @return array
45
     */
46
    protected function getHashingAlgorithms()
47
    {
48
49
        /** @var \gplcart\modules\oauth\Main $module */
50
        $module = $this->module->getInstance('oauth');
51
        return $module->getJwtHelper()->getAlgs();
52
    }
53
54
    /**
55
     * Set title on the module settings page
56
     */
57
    protected function setTitleEditSettings()
58
    {
59
        $title = $this->text('Edit %name settings', array('%name' => $this->text('API')));
60
        $this->setTitle($title);
61
    }
62
63
    /**
64
     * Set breadcrumbs on the module settings page
65
     */
66
    protected function setBreadcrumbEditSettings()
67
    {
68
        $breadcrumbs = array();
69
70
        $breadcrumbs[] = array(
71
            'text' => $this->text('Dashboard'),
72
            'url' => $this->url('admin')
73
        );
74
75
        $breadcrumbs[] = array(
76
            'text' => $this->text('Modules'),
77
            'url' => $this->url('admin/module/list')
78
        );
79
80
        $this->setBreadcrumbs($breadcrumbs);
81
    }
82
83
    /**
84
     * Saves the submitted settings
85
     */
86
    protected function submitSettings()
87
    {
88
        if ($this->isPosted('save') && $this->validateSettings()) {
89
            $this->updateSettings();
90
        }
91
    }
92
93
    /**
94
     * Validate submitted module settings
95
     * @return bool
96
     */
97
    protected function validateSettings()
98
    {
99
        $this->setSubmitted('settings');
100
        $this->setSubmittedBool('status');
101
102
        if ($this->getSubmitted('secret', '') === '') {
103
            $this->setSubmitted('secret', gplcart_string_random());
104
        }
105
106
        $this->validateElement('secret', 'length', array(8, 255));
107
        $this->validateElement('jwt_lifetime', 'regexp', '/^[\d]{2,4}$/');
108
109
        return !$this->hasErrors();
110
    }
111
112
    /**
113
     * Update module settings
114
     */
115
    protected function updateSettings()
116
    {
117
        $this->controlAccess('module_edit');
118
        $this->module->setSettings('api', $this->getSubmitted());
119
        $this->redirect('', $this->text('Settings have been updated'), 'success');
120
    }
121
122
    /**
123
     * Render and output the module settings page
124
     */
125
    protected function outputEditSettings()
126
    {
127
        $this->output('api|settings');
128
    }
129
}
130