Passed
Push — master ( ac8235...d58af6 )
by Andreas
20:06
created

dialog   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 97.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 38
c 1
b 0
f 0
dl 0
loc 94
ccs 45
cts 46
cp 0.9783
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A render_attributes() 0 11 2
A add_head_elements() 0 7 1
A configure() 0 2 1
A js_response() 0 6 1
A get_state() 0 3 1
A response() 0 5 1
A add_dialog_js() 0 4 1
A __construct() 0 7 2
A get_button() 0 17 5
1
<?php
2
/**
3
 * @package midcom.workflow
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\workflow;
10
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
use midcom;
13
use midcom_response_styled;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpFoundation\StreamedResponse;
17
18
/**
19
 * Workflow base class
20
 *
21
 * @package midcom.workflow
22
 */
23
abstract class dialog
24
{
25
    const INACTIVE = 'inactive';
26
27
    protected string $state = self::INACTIVE;
28
29 229
    public function __construct(array $options = [])
30
    {
31 229
        $resolver = new OptionsResolver();
32 229
        $this->configure($resolver);
33
34 229
        foreach ($resolver->resolve($options) as $key => $value) {
35 228
            $this->$key = $value;
36
        }
37
    }
38
39 131
    public function configure(OptionsResolver $resolver)
40
    {
41 131
    }
42
43 149
    public static function add_head_elements()
44
    {
45 149
        $head = midcom::get()->head;
46 149
        $head->enable_jquery_ui(['mouse', 'draggable', 'resizable', 'button', 'dialog']);
47 149
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/workflow.js');
48 149
        $head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css");
49 149
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.workflow/workflow.css');
50
    }
51
52 75
    protected function add_dialog_js()
53
    {
54 75
        midcom::get()->head->enable_jquery_ui(['button']);
55 75
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/dialog.js');
56
    }
57
58 75
    protected function response(\midcom_core_context $context) : midcom_response_styled
59
    {
60 75
        $this->add_dialog_js();
61 75
        midcom::get()->style->append_styledir(__DIR__ . '/style');
62 75
        return new midcom_response_styled($context, 'POPUP');
63
    }
64
65 29
    public function get_state() : string
66
    {
67 29
        return $this->state;
68
    }
69
70
    /**
71
     * @return array button config in midcom_helper_toolbar format
72
     */
73 148
    public function get_button(string $url, array $options = []) : array
74
    {
75 148
        static::add_head_elements();
76 148
        $button_config = $this->get_button_config();
77 148
        if (!empty($options[MIDCOM_TOOLBAR_ICON])) {
78
            unset($button_config[MIDCOM_TOOLBAR_GLYPHICON]);
79
        }
80 148
        $button_config[MIDCOM_TOOLBAR_URL] = $url;
81
        //The constants are numeric, so array_merge won't work...
82 148
        foreach ($options as $key => $value) {
83 146
            if (   is_array($value)
84 146
                && !empty($button_config[$key])) {
85 2
                $value = array_merge($button_config[$key], $value);
86
            }
87 146
            $button_config[$key] = $value;
88
        }
89 148
        return $button_config;
90
    }
91
92 7
    public function render_attributes() : string
93
    {
94 7
        $button_config = $this->get_button_config();
95
96 7
        $output = ' title="' . htmlspecialchars($button_config[MIDCOM_TOOLBAR_LABEL]) . '"';
97
98 7
        foreach ($button_config[MIDCOM_TOOLBAR_OPTIONS] as $key => $val) {
99 7
            $output .= ' ' . $key . '="' . htmlspecialchars($val) . '"';
100
        }
101
102 7
        return $output;
103
    }
104
105 14
    public function js_response(string $script) : Response
106
    {
107 14
        return new StreamedResponse(function() use ($script) {
108 8
            midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/dialog.js');
109 8
            midcom::get()->head->add_jscript($script);
110 8
            echo '<!DOCTYPE html><html><head>' . midcom::get()->head->render() . '</head><body></body></html>';
111 14
        });
112
    }
113
114
    abstract public function get_button_config() : array;
115
116
    abstract public function run(Request $request) : Response;
117
}
118