Settings::setTitleEditSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Shippo
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\shippo\controllers;
11
12
use Exception;
13
use gplcart\core\controllers\backend\Controller;
14
use gplcart\core\models\Country;
15
use gplcart\core\models\Shipping;
16
use gplcart\modules\shippo\models\Api;
17
18
/**
19
 * Handles incoming requests and outputs data related to Shippo module
20
 */
21
class Settings extends Controller
22
{
23
24
    /**
25
     * Shippo API model instance
26
     * @var \gplcart\modules\shippo\models\Api $api
27
     */
28
    protected $api;
29
30
    /**
31
     * Country model instance
32
     * @var \gplcart\core\models\Country $country
33
     */
34
    protected $country;
35
36
    /**
37
     * Shipping model instance
38
     * @var \gplcart\core\models\Shipping $shipping
39
     */
40
    protected $shipping;
41
42
    /**
43
     * Settings constructor.
44
     * @param Shipping $shipping
45
     * @param Country $country
46
     * @param Api $api
47
     */
48
    public function __construct(Shipping $shipping, Country $country, Api $api)
49
    {
50
        parent::__construct();
51
52
        $this->api = $api;
53
        $this->country = $country;
54
        $this->shipping = $shipping;
55
    }
56
57
    /**
58
     * Route page callback to display the module settings page
59
     */
60
    public function editSettings()
61
    {
62
        $this->setTitleEditSettings();
63
        $this->setBreadcrumbEditSettings();
64
65
        $this->setData('countries', $this->country->getIso());
66
        $this->setData('settings', $this->module->getSettings('shippo'));
67
        $this->setData('methods', $this->getShippingMethodsSettings());
68
69
        $this->submitSettings();
70
        $this->outputEditSettings();
71
    }
72
73
    /**
74
     * Returns an array of Shippo shipping methods
75
     * @return array
76
     */
77
    protected function getShippingMethodsSettings()
78
    {
79
        return $this->shipping->getList(array('module' => 'shippo'));
80
    }
81
82
    /**
83
     * Set title on the module settings page
84
     */
85
    protected function setTitleEditSettings()
86
    {
87
        $title = $this->text('Edit %name settings', array('%name' => $this->text('Shippo')));
88
        $this->setTitle($title);
89
    }
90
91
    /**
92
     * Set breadcrumbs on the module settings page
93
     */
94
    protected function setBreadcrumbEditSettings()
95
    {
96
        $breadcrumbs = array();
97
98
        $breadcrumbs[] = array(
99
            'text' => $this->text('Dashboard'),
100
            'url' => $this->url('admin')
101
        );
102
103
        $breadcrumbs[] = array(
104
            'text' => $this->text('Modules'),
105
            'url' => $this->url('admin/module/list')
106
        );
107
108
        $this->setBreadcrumbs($breadcrumbs);
109
    }
110
111
    /**
112
     * Saves the submitted settings
113
     */
114
    protected function submitSettings()
115
    {
116
        if ($this->isPosted('save') && $this->validateSettings()) {
117
            $this->updateSettings();
118
        }
119
    }
120
121
    /**
122
     * Validate the submitted module settings
123
     */
124
    protected function validateSettings()
125
    {
126
        try {
127
128
            $this->setSubmitted('settings');
129
130
            if ($this->api->isValidAddress($this->getSubmitted('sender')) !== true) {
131
                $this->setMessage($this->text("Shippo was unable to validate sender's address"), 'warning', true);
132
            }
133
134
        } catch (Exception $ex) {
135
            $this->setMessage($ex->getMessage(), 'warning', true);
136
        }
137
138
        return !$this->hasErrors();
139
    }
140
141
    /**
142
     * Update module settings
143
     */
144
    protected function updateSettings()
145
    {
146
        $this->controlAccess('module_edit');
147
        $this->module->setSettings('shippo', $this->getSubmitted());
148
        $this->redirect('', $this->text('Settings have been updated'), 'success');
149
    }
150
151
    /**
152
     * Render and output the module settings page
153
     */
154
    protected function outputEditSettings()
155
    {
156
        $this->output('shippo|settings');
157
    }
158
159
}
160