DialogPlugin   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 38
dl 0
loc 198
rs 10
c 1
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A info() 0 3 1
A getScript() 0 5 1
A getName() 0 3 1
A with() 0 4 1
A error() 0 3 1
A __construct() 0 4 1
A getLibraries() 0 7 2
A getUri() 0 3 1
A success() 0 3 1
A getCss() 0 5 1
A show() 0 5 1
A getReadyScript() 0 5 1
A getJs() 0 5 1
A getVersion() 0 3 1
A title() 0 4 1
A getSubdir() 0 3 1
A init() 0 3 1
A hide() 0 4 1
A warning() 0 3 1
A getHash() 0 4 1
1
<?php
2
3
/**
4
 * DialogPlugin.php - ModalInterface, message and question dialogs for Jaxon.
5
 *
6
 * Show modal, message and question dialogs with various javascript libraries
7
 * based on user settings.
8
 *
9
 * @package jaxon-core
10
 * @author Thierry Feuzeu <[email protected]>
11
 * @copyright 2016 Thierry Feuzeu <[email protected]>
12
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
13
 * @link https://github.com/jaxon-php/jaxon-core
14
 */
15
16
namespace Jaxon\Plugin\Response\Dialog;
17
18
use Jaxon\App\Dialog\AlertInterface;
19
use Jaxon\App\Dialog\ModalInterface;
20
use Jaxon\Exception\SetupException;
21
use Jaxon\Plugin\AbstractResponsePlugin;
22
23
use function array_reduce;
24
use function trim;
25
26
class DialogPlugin extends AbstractResponsePlugin implements ModalInterface, AlertInterface
27
{
28
    /**
29
     * @const The plugin name
30
     */
31
    const NAME = 'dialog';
32
33
    /**
34
     * @var DialogCommand
35
     */
36
    protected $xDialogCommand;
37
38
    /**
39
     * @var DialogManager
40
     */
41
    protected $xDialogManager;
42
43
    /**
44
     * @var array
45
     */
46
    protected $aLibraries = null;
47
48
    /**
49
     * The constructor
50
     *
51
     * @param DialogCommand $xDialogCommand
52
     * @param DialogManager $xDialogManager
53
     */
54
    public function __construct(DialogCommand $xDialogCommand, DialogManager $xDialogManager)
55
    {
56
        $this->xDialogCommand = $xDialogCommand;
57
        $this->xDialogManager = $xDialogManager;
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getName(): string
64
    {
65
        return self::NAME;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function getHash(): string
72
    {
73
        // The version number is used as hash
74
        return '4.0.0';
75
    }
76
77
    public function getUri(): string
78
    {
79
        return '';
80
    }
81
82
    public function getSubdir(): string
83
    {
84
        return '';
85
    }
86
87
    public function getVersion(): string
88
    {
89
        return '';
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95
    protected function init()
96
    {
97
        $this->xDialogManager->setNextLibrary('');
98
    }
99
100
    /**
101
     * Set the library to use for the next call.
102
     *
103
     * @param string $sLibrary The name of the library
104
     *
105
     * @return DialogPlugin
106
     */
107
    public function with(string $sLibrary): DialogPlugin
108
    {
109
        $this->xDialogManager->setNextLibrary($sLibrary);
110
        return $this;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    private function getLibraries(): array
117
    {
118
        if($this->aLibraries === null)
119
        {
120
            $this->aLibraries = $this->xDialogManager->getLibraries();
121
        }
122
        return $this->aLibraries;
123
    }
124
125
    /**
126
     * @inheritDoc
127
     */
128
    public function getJs(): string
129
    {
130
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
131
            return $sCode . $xLibrary->getJs() . "\n\n";
132
        }, '');
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function getCss(): string
139
    {
140
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
141
            return $sCode . trim($xLibrary->getCss()) . "\n\n";
142
        }, '');
143
    }
144
145
    /**
146
     * @inheritDoc
147
     * @throws SetupException
148
     */
149
    public function getScript(): string
150
    {
151
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
152
            return $sCode . trim($xLibrary->getScript()) . "\n\n";
153
        }, '');
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function getReadyScript(): string
160
    {
161
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
162
            return $sCode . trim($xLibrary->getReadyScript()) . "\n\n";
163
        }, '');
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    public function show(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = [])
170
    {
171
        // Show the modal dialog
172
        $this->addCommand('dialog.modal.show',
173
            $this->xDialogCommand->show($sTitle, $sContent, $aButtons, $aOptions));
174
    }
175
176
    /**
177
     * @inheritDoc
178
     */
179
    public function hide()
180
    {
181
        // Hide the modal dialog
182
        $this->addCommand('dialog.modal.hide', $this->xDialogCommand->hide());
183
    }
184
185
    /**
186
     * @inheritDoc
187
     */
188
    public function title(string $sTitle): AlertInterface
189
    {
190
        $this->xDialogCommand->title($sTitle);
191
        return $this;
192
    }
193
194
    /**
195
     * @inheritDoc
196
     */
197
    public function success(string $sMessage, array $aArgs = [])
198
    {
199
        $this->addCommand('dialog.alert.show', $this->xDialogCommand->success($sMessage, $aArgs));
200
    }
201
202
    /**
203
     * @inheritDoc
204
     */
205
    public function info(string $sMessage, array $aArgs = [])
206
    {
207
        $this->addCommand('dialog.alert.show', $this->xDialogCommand->info($sMessage, $aArgs));
208
    }
209
210
    /**
211
     * @inheritDoc
212
     */
213
    public function warning(string $sMessage, array $aArgs = [])
214
    {
215
        $this->addCommand('dialog.alert.show', $this->xDialogCommand->warning($sMessage, $aArgs));
216
    }
217
218
    /**
219
     * @inheritDoc
220
     */
221
    public function error(string $sMessage, array $aArgs = [])
222
    {
223
        $this->addCommand('dialog.alert.show', $this->xDialogCommand->error($sMessage, $aArgs));
224
    }
225
}
226